-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathGekkoIRGen.cpp
835 lines (740 loc) · 20.4 KB
/
GekkoIRGen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
// Copyright 2023 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "Common/Assembler/GekkoIRGen.h"
#include <bit>
#include <functional>
#include <map>
#include <numeric>
#include <set>
#include <stack>
#include <variant>
#include <vector>
#include <fmt/format.h>
#include "Common/Assembler/AssemblerShared.h"
#include "Common/Assembler/GekkoParser.h"
#include "Common/Assert.h"
#include "Common/BitUtils.h"
namespace Common::GekkoAssembler::detail
{
namespace
{
class GekkoIRPlugin : public ParsePlugin
{
public:
GekkoIRPlugin(GekkoIR& result, u32 base_addr)
: m_output_result(result), m_active_var(nullptr), m_operand_scan_begin(0)
{
m_active_block = &m_output_result.blocks.emplace_back(base_addr);
}
virtual ~GekkoIRPlugin() = default;
void OnDirectivePre(GekkoDirective directive) override;
void OnDirectivePost(GekkoDirective directive) override;
void OnInstructionPre(const ParseInfo& mnemonic_info, bool extended) override;
void OnInstructionPost(const ParseInfo& mnemonic_info, bool extended) override;
void OnOperandPre() override;
void OnOperandPost() override;
void OnResolvedExprPost() override;
void OnOperator(AsmOp operation) override;
void OnTerminal(Terminal type, const AssemblerToken& val) override;
void OnHiaddr(std::string_view id) override;
void OnLoaddr(std::string_view id) override;
void OnCloseParen(ParenType type) override;
void OnLabelDecl(std::string_view name) override;
void OnVarDecl(std::string_view name) override;
void PostParseAction() override;
u32 CurrentAddress() const;
std::optional<u64> LookupVar(std::string_view lab);
std::optional<u32> LookupLabel(std::string_view lab);
template <typename T>
T& GetChunk();
template <typename T>
void AddBytes(T val);
void AddStringBytes(std::string_view str, bool null_term);
void PadAlign(u32 bits);
void PadSpace(size_t space);
void StartBlock(u32 address);
void StartBlockAlign(u32 bits);
void StartInstruction(size_t mnemonic_index, bool extended);
void FinishInstruction();
void SaveOperandFixup(size_t str_left, size_t str_right);
void AddBinaryEvaluator(u32 (*evaluator)(u32, u32));
void AddUnaryEvaluator(u32 (*evaluator)(u32));
void AddAbsoluteAddressConv();
void AddLiteral(u32 lit);
void AddSymbolResolve(std::string_view sym, bool absolute);
void RunFixups();
void EvalOperatorRel(AsmOp operation);
void EvalOperatorAbs(AsmOp operation);
void EvalTerminalRel(Terminal type, const AssemblerToken& tok);
void EvalTerminalAbs(Terminal type, const AssemblerToken& tok);
private:
enum class EvalMode
{
RelAddrDoublePass,
AbsAddrSinglePass,
};
GekkoIR& m_output_result;
IRBlock* m_active_block;
GekkoInstruction m_build_inst;
u64* m_active_var;
size_t m_operand_scan_begin;
std::map<std::string, u32, std::less<>> m_labels;
std::map<std::string, u64, std::less<>> m_constants;
std::set<std::string> m_symset;
EvalMode m_evaluation_mode;
// For operand parsing
std::stack<std::function<u32()>> m_fixup_stack;
std::vector<std::function<u32()>> m_operand_fixups;
size_t m_operand_str_start;
// For directive parsing
std::vector<u64> m_eval_stack;
std::variant<std::vector<float>, std::vector<double>> m_floats_list;
std::string_view m_string_lit;
GekkoDirective m_active_directive;
};
///////////////
// OVERRIDES //
///////////////
void GekkoIRPlugin::OnDirectivePre(GekkoDirective directive)
{
m_evaluation_mode = EvalMode::AbsAddrSinglePass;
m_active_directive = directive;
m_eval_stack = std::vector<u64>{};
switch (directive)
{
case GekkoDirective::Float:
m_floats_list = std::vector<float>{};
break;
case GekkoDirective::Double:
m_floats_list = std::vector<double>{};
break;
default:
break;
}
}
void GekkoIRPlugin::OnDirectivePost(GekkoDirective directive)
{
switch (directive)
{
// .nbyte directives are handled by OnResolvedExprPost
default:
break;
case GekkoDirective::Float:
case GekkoDirective::Double:
std::visit(
[this](auto&& vec) {
for (auto&& val : vec)
{
AddBytes(val);
}
},
m_floats_list);
break;
case GekkoDirective::DefVar:
ASSERT(m_active_var != nullptr);
*m_active_var = m_eval_stack.back();
m_active_var = nullptr;
break;
case GekkoDirective::Locate:
StartBlock(static_cast<u32>(m_eval_stack.back()));
break;
case GekkoDirective::Zeros:
PadSpace(static_cast<u32>(m_eval_stack.back()));
break;
case GekkoDirective::Skip:
{
const u32 skip_len = static_cast<u32>(m_eval_stack.back());
if (skip_len > 0)
{
StartBlock(CurrentAddress() + skip_len);
}
break;
}
case GekkoDirective::PadAlign:
PadAlign(static_cast<u32>(m_eval_stack.back()));
break;
case GekkoDirective::Align:
StartBlockAlign(static_cast<u32>(m_eval_stack.back()));
break;
case GekkoDirective::Ascii:
AddStringBytes(m_string_lit, false);
break;
case GekkoDirective::Asciz:
AddStringBytes(m_string_lit, true);
break;
}
m_eval_stack = {};
}
void GekkoIRPlugin::OnInstructionPre(const ParseInfo& mnemonic_info, bool extended)
{
m_evaluation_mode = EvalMode::RelAddrDoublePass;
StartInstruction(mnemonic_info.mnemonic_index, extended);
}
void GekkoIRPlugin::OnInstructionPost(const ParseInfo&, bool)
{
FinishInstruction();
}
void GekkoIRPlugin::OnOperandPre()
{
m_operand_str_start = m_owner->lexer.ColNumber();
}
void GekkoIRPlugin::OnOperandPost()
{
SaveOperandFixup(m_operand_str_start, m_owner->lexer.ColNumber());
}
void GekkoIRPlugin::OnResolvedExprPost()
{
switch (m_active_directive)
{
case GekkoDirective::Byte:
AddBytes<u8>(static_cast<u8>(m_eval_stack.back()));
break;
case GekkoDirective::_2byte:
AddBytes<u16>(static_cast<u16>(m_eval_stack.back()));
break;
case GekkoDirective::_4byte:
AddBytes<u32>(static_cast<u32>(m_eval_stack.back()));
break;
case GekkoDirective::_8byte:
AddBytes<u64>(static_cast<u64>(m_eval_stack.back()));
break;
default:
return;
}
m_eval_stack.clear();
}
void GekkoIRPlugin::OnOperator(AsmOp operation)
{
if (m_evaluation_mode == EvalMode::RelAddrDoublePass)
{
EvalOperatorRel(operation);
}
else
{
EvalOperatorAbs(operation);
}
}
void GekkoIRPlugin::OnTerminal(Terminal type, const AssemblerToken& val)
{
if (type == Terminal::Str)
{
m_string_lit = val.token_val;
}
else if (m_evaluation_mode == EvalMode::RelAddrDoublePass)
{
EvalTerminalRel(type, val);
}
else
{
EvalTerminalAbs(type, val);
}
}
void GekkoIRPlugin::OnHiaddr(std::string_view id)
{
if (m_evaluation_mode == EvalMode::RelAddrDoublePass)
{
AddSymbolResolve(id, true);
AddLiteral(16);
AddBinaryEvaluator([](u32 lhs, u32 rhs) { return lhs >> rhs; });
AddLiteral(0xffff);
AddBinaryEvaluator([](u32 lhs, u32 rhs) { return lhs & rhs; });
}
else
{
u32 base;
if (auto lbl = LookupLabel(id); lbl)
{
base = *lbl;
}
else if (auto var = LookupVar(id); var)
{
base = *var;
}
else
{
m_owner->EmitErrorHere(fmt::format("Undefined reference to Label/Constant '{}'", id));
return;
}
m_eval_stack.push_back((base >> 16) & 0xffff);
}
}
void GekkoIRPlugin::OnLoaddr(std::string_view id)
{
if (m_evaluation_mode == EvalMode::RelAddrDoublePass)
{
AddSymbolResolve(id, true);
AddLiteral(0xffff);
AddBinaryEvaluator([](u32 lhs, u32 rhs) { return lhs & rhs; });
}
else
{
u32 base;
if (auto lbl = LookupLabel(id); lbl)
{
base = *lbl;
}
else if (auto var = LookupVar(id); var)
{
base = *var;
}
else
{
m_owner->EmitErrorHere(fmt::format("Undefined reference to Label/Constant '{}'", id));
return;
}
m_eval_stack.push_back(base & 0xffff);
}
}
void GekkoIRPlugin::OnCloseParen(ParenType type)
{
if (type != ParenType::RelConv)
{
return;
}
if (m_evaluation_mode == EvalMode::RelAddrDoublePass)
{
AddAbsoluteAddressConv();
}
else
{
m_eval_stack.push_back(CurrentAddress());
EvalOperatorAbs(AsmOp::Sub);
}
}
void GekkoIRPlugin::OnLabelDecl(std::string_view name)
{
const std::string name_str(name);
if (m_symset.contains(name_str))
{
m_owner->EmitErrorHere(fmt::format("Label/Constant {} is already defined", name));
return;
}
m_labels[name_str] = m_active_block->BlockEndAddress();
m_symset.insert(name_str);
}
void GekkoIRPlugin::OnVarDecl(std::string_view name)
{
const std::string name_str(name);
if (m_symset.contains(name_str))
{
m_owner->EmitErrorHere(fmt::format("Label/Constant {} is already defined", name));
return;
}
m_active_var = &m_constants[name_str];
m_symset.insert(name_str);
}
void GekkoIRPlugin::PostParseAction()
{
RunFixups();
}
//////////////////////
// HELPER FUNCTIONS //
//////////////////////
u32 GekkoIRPlugin::CurrentAddress() const
{
return m_active_block->BlockEndAddress();
}
std::optional<u64> GekkoIRPlugin::LookupVar(std::string_view var)
{
auto var_it = m_constants.find(var);
return var_it == m_constants.end() ? std::nullopt : std::optional(var_it->second);
}
std::optional<u32> GekkoIRPlugin::LookupLabel(std::string_view lab)
{
auto label_it = m_labels.find(lab);
return label_it == m_labels.end() ? std::nullopt : std::optional(label_it->second);
}
void GekkoIRPlugin::AddStringBytes(std::string_view str, bool null_term)
{
ByteChunk& bytes = GetChunk<ByteChunk>();
ConvertStringLiteral(str, &bytes);
if (null_term)
{
bytes.push_back('\0');
}
}
template <typename T>
T& GekkoIRPlugin::GetChunk()
{
if (!m_active_block->chunks.empty() && std::holds_alternative<T>(m_active_block->chunks.back()))
{
return std::get<T>(m_active_block->chunks.back());
}
return std::get<T>(m_active_block->chunks.emplace_back(T{}));
}
template <typename T>
void GekkoIRPlugin::AddBytes(T val)
{
if constexpr (std::is_integral_v<T>)
{
ByteChunk& bytes = GetChunk<ByteChunk>();
for (size_t i = sizeof(T) - 1; i > 0; i--)
{
bytes.push_back((val >> (8 * i)) & 0xff);
}
bytes.push_back(val & 0xff);
}
else if constexpr (std::is_same_v<T, float>)
{
static_assert(sizeof(double) == sizeof(u64));
AddBytes(std::bit_cast<u32>(val));
}
else
{
// std::is_same_v<T, double>
static_assert(sizeof(double) == sizeof(u64));
AddBytes(std::bit_cast<u64>(val));
}
}
void GekkoIRPlugin::PadAlign(u32 bits)
{
const u32 align_mask = (1 << bits) - 1;
const u32 current_addr = m_active_block->BlockEndAddress();
if (current_addr & align_mask)
{
PadChunk& current_pad = GetChunk<PadChunk>();
current_pad += (1 << bits) - (current_addr & align_mask);
}
}
void GekkoIRPlugin::PadSpace(size_t space)
{
GetChunk<PadChunk>() += space;
}
void GekkoIRPlugin::StartBlock(u32 address)
{
m_active_block = &m_output_result.blocks.emplace_back(address);
}
void GekkoIRPlugin::StartBlockAlign(u32 bits)
{
const u32 align_mask = (1 << bits) - 1;
const u32 current_addr = m_active_block->BlockEndAddress();
if (current_addr & align_mask)
{
StartBlock((1 << bits) + (current_addr & ~align_mask));
}
}
void GekkoIRPlugin::StartInstruction(size_t mnemonic_index, bool extended)
{
m_build_inst = GekkoInstruction{
.mnemonic_index = mnemonic_index,
.raw_text = m_owner->lexer.CurrentLine(),
.line_number = m_owner->lexer.LineNumber(),
.is_extended = extended,
};
m_operand_scan_begin = m_output_result.operand_pool.size();
}
void GekkoIRPlugin::AddBinaryEvaluator(u32 (*evaluator)(u32, u32))
{
std::function<u32()> rhs = std::move(m_fixup_stack.top());
m_fixup_stack.pop();
std::function<u32()> lhs = std::move(m_fixup_stack.top());
m_fixup_stack.pop();
m_fixup_stack.emplace([evaluator, lhs = std::move(lhs), rhs = std::move(rhs)]() {
return evaluator(lhs(), rhs());
});
}
void GekkoIRPlugin::AddUnaryEvaluator(u32 (*evaluator)(u32))
{
std::function<u32()> sub = std::move(m_fixup_stack.top());
m_fixup_stack.pop();
m_fixup_stack.emplace([evaluator, sub = std::move(sub)]() { return evaluator(sub()); });
}
void GekkoIRPlugin::AddAbsoluteAddressConv()
{
const u32 inst_address = m_active_block->BlockEndAddress();
std::function<u32()> sub = std::move(m_fixup_stack.top());
m_fixup_stack.pop();
m_fixup_stack.emplace([inst_address, sub = std::move(sub)] { return sub() - inst_address; });
}
void GekkoIRPlugin::AddLiteral(u32 lit)
{
m_fixup_stack.emplace([lit] { return lit; });
}
void GekkoIRPlugin::AddSymbolResolve(std::string_view sym, bool absolute)
{
const u32 source_address = m_active_block->BlockEndAddress();
AssemblerError err_on_fail = AssemblerError{
fmt::format("Unresolved symbol '{}'", sym),
m_owner->lexer.CurrentLine(),
m_owner->lexer.LineNumber(),
// Lexer should currently point to the label, as it hasn't been eaten yet
m_owner->lexer.ColNumber(),
sym.size(),
};
m_fixup_stack.emplace(
[this, sym, absolute, source_address, err_on_fail = std::move(err_on_fail)] {
auto label_it = m_labels.find(sym);
if (label_it != m_labels.end())
{
if (absolute)
{
return label_it->second;
}
return label_it->second - source_address;
}
auto var_it = m_constants.find(sym);
if (var_it != m_constants.end())
{
return static_cast<u32>(var_it->second);
}
m_owner->error = std::move(err_on_fail);
return u32{0};
});
}
void GekkoIRPlugin::SaveOperandFixup(size_t str_left, size_t str_right)
{
m_operand_fixups.emplace_back(std::move(m_fixup_stack.top()));
m_fixup_stack.pop();
m_output_result.operand_pool.emplace_back(Interval{str_left, str_right - str_left}, 0);
}
void GekkoIRPlugin::RunFixups()
{
for (size_t i = 0; i < m_operand_fixups.size(); i++)
{
ValueOf(m_output_result.operand_pool[i]) = m_operand_fixups[i]();
if (m_owner->error)
{
return;
}
}
}
void GekkoIRPlugin::FinishInstruction()
{
m_build_inst.op_interval.begin = m_operand_scan_begin;
m_build_inst.op_interval.len = m_output_result.operand_pool.size() - m_operand_scan_begin;
GetChunk<InstChunk>().emplace_back(m_build_inst);
m_operand_scan_begin = 0;
}
void GekkoIRPlugin::EvalOperatorAbs(AsmOp operation)
{
#define EVAL_BINARY_OP(OPERATOR) \
{ \
u64 rhs = m_eval_stack.back(); \
m_eval_stack.pop_back(); \
m_eval_stack.back() = m_eval_stack.back() OPERATOR rhs; \
}
switch (operation)
{
case AsmOp::Or:
EVAL_BINARY_OP(|);
break;
case AsmOp::Xor:
EVAL_BINARY_OP(^);
break;
case AsmOp::And:
EVAL_BINARY_OP(&);
break;
case AsmOp::Lsh:
EVAL_BINARY_OP(<<);
break;
case AsmOp::Rsh:
EVAL_BINARY_OP(>>);
break;
case AsmOp::Add:
EVAL_BINARY_OP(+);
break;
case AsmOp::Sub:
EVAL_BINARY_OP(-);
break;
case AsmOp::Mul:
EVAL_BINARY_OP(*);
break;
case AsmOp::Div:
EVAL_BINARY_OP(/);
break;
case AsmOp::Neg:
m_eval_stack.back() = static_cast<u32>(-static_cast<s32>(m_eval_stack.back()));
break;
case AsmOp::Not:
m_eval_stack.back() = ~m_eval_stack.back();
break;
}
#undef EVAL_BINARY_OP
#undef EVAL_UNARY_OP
}
void GekkoIRPlugin::EvalOperatorRel(AsmOp operation)
{
switch (operation)
{
case AsmOp::Or:
AddBinaryEvaluator([](u32 lhs, u32 rhs) { return lhs | rhs; });
break;
case AsmOp::Xor:
AddBinaryEvaluator([](u32 lhs, u32 rhs) { return lhs ^ rhs; });
break;
case AsmOp::And:
AddBinaryEvaluator([](u32 lhs, u32 rhs) { return lhs & rhs; });
break;
case AsmOp::Lsh:
AddBinaryEvaluator([](u32 lhs, u32 rhs) { return lhs << rhs; });
break;
case AsmOp::Rsh:
AddBinaryEvaluator([](u32 lhs, u32 rhs) { return lhs >> rhs; });
break;
case AsmOp::Add:
AddBinaryEvaluator([](u32 lhs, u32 rhs) { return lhs + rhs; });
break;
case AsmOp::Sub:
AddBinaryEvaluator([](u32 lhs, u32 rhs) { return lhs - rhs; });
break;
case AsmOp::Mul:
AddBinaryEvaluator([](u32 lhs, u32 rhs) { return lhs * rhs; });
break;
case AsmOp::Div:
AddBinaryEvaluator([](u32 lhs, u32 rhs) { return lhs / rhs; });
break;
case AsmOp::Neg:
AddUnaryEvaluator([](u32 val) { return static_cast<u32>(-static_cast<s32>(val)); });
break;
case AsmOp::Not:
AddUnaryEvaluator([](u32 val) { return ~val; });
break;
}
}
void GekkoIRPlugin::EvalTerminalRel(Terminal type, const AssemblerToken& tok)
{
switch (type)
{
case Terminal::Hex:
case Terminal::Dec:
case Terminal::Oct:
case Terminal::Bin:
case Terminal::GPR:
case Terminal::FPR:
case Terminal::GQR:
case Terminal::SPR:
case Terminal::CRField:
case Terminal::Lt:
case Terminal::Gt:
case Terminal::Eq:
case Terminal::So:
{
std::optional<u32> val = tok.EvalToken<u32>();
ASSERT(val.has_value());
AddLiteral(*val);
break;
}
case Terminal::Dot:
AddLiteral(CurrentAddress());
break;
case Terminal::Id:
{
if (auto label_it = m_labels.find(tok.token_val); label_it != m_labels.end())
{
AddLiteral(label_it->second - CurrentAddress());
}
else if (auto var_it = m_constants.find(tok.token_val); var_it != m_constants.end())
{
AddLiteral(var_it->second);
}
else
{
AddSymbolResolve(tok.token_val, false);
}
break;
}
// Parser should disallow this from happening
default:
ASSERT(false);
break;
}
}
void GekkoIRPlugin::EvalTerminalAbs(Terminal type, const AssemblerToken& tok)
{
switch (type)
{
case Terminal::Hex:
case Terminal::Dec:
case Terminal::Oct:
case Terminal::Bin:
case Terminal::GPR:
case Terminal::FPR:
case Terminal::GQR:
case Terminal::SPR:
case Terminal::CRField:
case Terminal::Lt:
case Terminal::Gt:
case Terminal::Eq:
case Terminal::So:
{
std::optional<u64> val = tok.EvalToken<u64>();
ASSERT(val.has_value());
m_eval_stack.push_back(*val);
break;
}
case Terminal::Flt:
{
std::visit(
[&tok](auto&& vec) {
auto opt = tok.EvalToken<typename std::decay_t<decltype(vec)>::value_type>();
ASSERT(opt.has_value());
vec.push_back(*opt);
},
m_floats_list);
break;
}
case Terminal::Dot:
m_eval_stack.push_back(static_cast<u64>(CurrentAddress()));
break;
case Terminal::Id:
{
if (auto label_it = m_labels.find(tok.token_val); label_it != m_labels.end())
{
m_eval_stack.push_back(label_it->second);
}
else if (auto var_it = m_constants.find(tok.token_val); var_it != m_constants.end())
{
m_eval_stack.push_back(var_it->second);
}
else
{
m_owner->EmitErrorHere(
fmt::format("Undefined reference to Label/Constant '{}'", tok.ValStr()));
return;
}
break;
}
// Parser should disallow this from happening
default:
ASSERT(false);
break;
}
}
} // namespace
u32 IRBlock::BlockEndAddress() const
{
return std::accumulate(chunks.begin(), chunks.end(), block_address,
[](u32 acc, const ChunkVariant& chunk) {
size_t size;
if (std::holds_alternative<InstChunk>(chunk))
{
size = std::get<InstChunk>(chunk).size() * 4;
}
else if (std::holds_alternative<ByteChunk>(chunk))
{
size = std::get<ByteChunk>(chunk).size();
}
else if (std::holds_alternative<PadChunk>(chunk))
{
size = std::get<PadChunk>(chunk);
}
else
{
ASSERT(false);
size = 0;
}
return acc + static_cast<u32>(size);
});
}
FailureOr<GekkoIR> ParseToIR(std::string_view assembly, u32 base_virtual_address)
{
GekkoIR ret;
GekkoIRPlugin plugin(ret, base_virtual_address);
ParseWithPlugin(&plugin, assembly);
if (plugin.Error())
{
return FailureOr<GekkoIR>(std::move(*plugin.Error()));
}
return std::move(ret);
}
} // namespace Common::GekkoAssembler::detail