-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmachine.cpp
2545 lines (2151 loc) · 46.3 KB
/
machine.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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// machine.cpp
// Revision 22-nov-2006
#include "machine.h"
#include "asmimpl.h"
#include "var.h"
#include "token.h"
#include "parser.h"
#include "parsertypes.h"
#include "trace.h"
#include <sstream>
#include <deque>
#include <stdexcept>
namespace pasmo {
namespace impl {
using std::ostringstream;
using std::deque;
using std::logic_error;
using std::runtime_error;
namespace {
string showtoken (const Token & tok)
{
const string stok ('\'' + tok.str () + '\'');
string s;
TypeToken tt= tok.type ();
switch (tt)
{
case TypeEndLine: s= "(end of line)"; break;
case TypeIdentifier: s= "Identifier " + stok; break;
case TypeMacroName: s= "Macro " + stok; break;
case TypeLiteral: s= "Literal " + stok; break;
case TypeNumber: s= "Number " + stok; break;
default: s= stok;
}
return s;
}
const string messexpected (" expected but ");
const string messfound (" found");
string showfound (const Token & tokfound)
{
return messexpected + showtoken (tokfound) + messfound;
}
string showexpfound (const Token & tokexp, const Token & tokfound)
{
return showtoken (tokexp) + showfound (tokfound);
}
string name_of (const VarData & vd)
{
TRF;
const string & name= vd.getname ();
if (name.size () > 0)
return " of " + name + ' ';
else
return string (1, ' ');
}
} // namespace
//**************************************************************
// Assembler errors
//**************************************************************
nonfatal_error DivisionByZero ("Division by zero");
nonfatal_error Lenght1Required ("Invalid literal, length 1 required");
nonfatal_error InvalidOperand ("Invalid operand");
class Expected : public nonfatal_error {
public:
Expected (const Token & tokexp, const Token & tokfound) :
nonfatal_error (showexpfound (tokexp, tokfound) )
{ }
Expected (const string & expected, const Token & tok) :
nonfatal_error (expected + showfound (tok) )
{ }
Expected (const string & expected, const string & found) :
nonfatal_error (expected + messexpected + found + messfound)
{ }
};
class Unexpected : public nonfatal_error {
public:
Unexpected (const Token & tokfound, const string & str) :
nonfatal_error ("Unexpected " +
showtoken (tokfound) + ' ' + str)
{ }
};
class Undefined : public nonfatal_error {
public:
Undefined (const VarData & vd) :
nonfatal_error ("Value" + name_of (vd) + "undefined")
{ }
};
void check_defined (const VarData & vd, bool required)
{
TRF;
if (required && ! vd.isdefined () )
{
//throw nonfatal_error ("Value undefined");
throw Undefined (vd);
}
}
void check_defined_noextern (const VarData & vd, bool required)
{
vd.noextern ();
check_defined (vd, required);
}
//**************************************************************
typedef VarData MachValueType;
class MachStack {
public:
bool empty () const;
MachValueType result ();
MachValueType pop ();
void push (address n);
void push (const Value & v);
void push (const VarData & vd);
void defined ();
void add ();
void sub ();
void mul ();
void div ();
void mod ();
void shl ();
void shr ();
void equal ();
void notequal ();
void lessthan ();
void greaterthan ();
void lessequal ();
void greaterequal ();
void do_not ();
void do_boolnot ();
void do_minus ();
void do_and ();
void do_or ();
void do_xor ();
void booland ();
void boolor ();
void do_high ();
void do_low ();
void cond ();
private:
//typedef deque <MachValue> st_t;
typedef deque <MachValueType> st_t;
st_t st;
//MachValue & back ();
//MachValue & get (size_t n);
MachValueType & back ();
MachValueType & get (size_t n);
void drop ();
void drop (size_t n);
};
bool MachStack::empty () const
{
return st.empty ();
}
VarData & MachStack::back ()
{
ASSERT (! st.empty () );
return st.back ();
}
void MachStack::drop ()
{
st.pop_back ();
}
void MachStack::drop (size_t n)
{
ASSERT (st.size () >= n);
for (size_t i= 0; i < n; ++i)
st.pop_back ();
}
VarData & MachStack::get (size_t n)
{
ASSERT (n > 0);
st_t::size_type s= st.size ();
ASSERT (n < s);
return st [s - n - 1];
}
MachValueType MachStack::result ()
{
TRF;
MachValueType v= back ();
drop ();
if (! st.empty () )
throw logic_error ("Evaluator stack unbalanced");
return v;
}
MachValueType MachStack::pop ()
{
TRF;
MachValueType v= back ();
if (v.errordiv () )
throw DivisionByZero;
drop ();
return v;
}
void MachStack::push (address n)
{
st.push_back (MachValueType (n, PreDefined) );
}
void MachStack::push (const Value & v)
{
st.push_back (MachValueType (v, PreDefined) );
}
void MachStack::push (const VarData & vd)
{
st.push_back (vd);
}
void MachStack::defined ()
{
back ().setdefined ();
}
void MachStack::add ()
{
TRFUNC (tr, "MachStack::add");
//get (1).add (back () );
VarData & op1= get (1);
op1.add (back () );
drop ();
}
void MachStack::sub ()
{
get (1).sub (back () );
drop ();
}
void MachStack::mul ()
{
TRFUNC (tr, "MachStack::mul");
get (1).mul (back () );
drop ();
}
void MachStack::div ()
{
get (1).div (back () );
drop ();
}
void MachStack::mod ()
{
get (1).mod (back () );
drop ();
}
void MachStack::shl ()
{
get (1).shl (back () );
drop ();
}
void MachStack::shr ()
{
get (1).shr (back () );
drop ();
}
void MachStack::equal ()
{
get (1).equal (back () );
drop ();
}
void MachStack::notequal ()
{
get (1).notequal (back () );
drop ();
}
void MachStack::lessthan ()
{
get (1).lessthan (back () );
drop ();
}
void MachStack::greaterthan ()
{
get (1).greaterthan (back () );
drop ();
}
void MachStack::lessequal ()
{
get (1).lessequal (back () );
drop ();
}
void MachStack::greaterequal ()
{
get (1).greaterequal (back () );
drop ();
}
void MachStack::do_not ()
{
back ().do_not ();
}
void MachStack::do_boolnot ()
{
back ().do_boolnot ();
}
void MachStack::do_minus ()
{
back ().do_minus ();
}
void MachStack::do_and ()
{
TRFUNC (tr, "MachStack::and");
get (1).do_and (back () );
drop ();
}
void MachStack::do_or ()
{
get (1).do_or (back () );
drop ();
}
void MachStack::do_xor ()
{
get (1).do_xor (back () );
drop ();
}
void MachStack::booland ()
{
get (1).booland (back () );
drop ();
}
void MachStack::boolor ()
{
get (1).boolor (back () );
drop ();
}
void MachStack::do_high ()
{
TRFUNC (tr, "MachStack::high");
back ().do_high ();
}
void MachStack::do_low ()
{
TRFUNC (tr, "MachStack::low");
back ().do_low ();
}
void MachStack::cond ()
{
get (2).cond (get (1), back () );
drop (2);
}
class Instruc {
public:
Instruc (Oper op_n);
Instruc (Oper op_n, const string & lit);
Instruc (address value_n);
Instruc (const string & ident_n);
Instruc (const Instruc & instruc);
~Instruc ();
void operator = (const Instruc & instruc);
Oper getop () const;
address getvalue () const;
string getident () const;
string getliteral () const;
private:
Oper op;
union V {
address value;
string * pident;
};
V v;
};
Instruc::Instruc (Oper op_n) :
op (op_n)
{ }
Instruc::Instruc (Oper op_n, const string & lit) :
op (op_n)
{
ASSERT (op == OpLiteral);
v.pident= new string (lit);
}
Instruc::Instruc (address value_n) :
op (OpNumber)
{
v.value= value_n;
}
Instruc::Instruc (const string & ident_n) :
op (OpIdentifier)
{
v.pident= new string (ident_n);
}
Instruc::Instruc (const Instruc & instruc) :
op (instruc.op)
{
switch (op)
{
case OpIdentifier:
case OpLiteral:
v.pident= new string (* (instruc.v.pident) );
break;
case OpNumber:
v.value= instruc.v.value;
break;
default:
; // Nothing.
}
}
Instruc::~Instruc ()
{
switch (op)
{
case OpIdentifier:
case OpLiteral:
delete v.pident;
break;
default:
; // nothing
}
}
void Instruc::operator = (const Instruc & instruc)
{
if (this != & instruc)
{
if (op == OpIdentifier)
delete v.pident;
op= instruc.op;
if (op == OpIdentifier)
v.pident= new string (* (instruc.v.pident) );
}
}
Oper Instruc::getop () const
{
return op;
}
address Instruc::getvalue () const
{
switch (op)
{
case OpNumber:
return v.value;
case OpLiteral:
{
const string & s= * v.pident;
const string::size_type l= s.size ();
switch (l)
{
case 1:
return static_cast <unsigned char>
(s [0] );
case 2:
return static_cast <unsigned char> (s [0] )
* 256 +
static_cast <unsigned char> (s [1] );
default:
throw Lenght1Required;
}
}
default:
throw logic_error ("Invalid parser machine operation");
}
}
string Instruc::getident () const
{
switch (op)
{
case OpIdentifier:
return * (v.pident);
default:
throw logic_error ("Invalid parser machine operation");
}
}
string Instruc::getliteral () const
{
switch (op)
{
case OpLiteral:
return * (v.pident);
default:
throw logic_error ("Invalid parser machine operation");
}
}
class AsmMachine : public Machine {
public:
AsmMachine (AsmImpl & asmin_n, Tokenizer & tz_n,
ParseType pt_n, bool inpass2_n);
virtual ~AsmMachine () { }
void syntax (const char * s);
void ugly ();
Token gettoken ();
void expectmacro ();
void addcode (Oper op);
void addcode (address value);
void addcode (const string & ident);
void addcodeliteral (const string & lit);
void addlabel (const string & labelname);
void addmacroname (const Token & tok);
void cancelmacroname ();
void redefmacro ();
void macroargs ();
void nomacroargs ();
void addmacroitem (const Token & tok);
void errmsg (const string & errmsg);
void invalid ();
void expected (const Token & tokexp, const Token & tokfound);
void unexpected (const Token & tokfound, const string & str);
void exec ();
private:
AsmImpl & asmin;
bool nocase;
typedef deque <Instruc> inst_t;
inst_t inst;
MachStack st;
Tokenizer & tz;
ParseType pt;
bool require_defined;
bool expectingmacro;
VarnameList varlist;
string label;
bool stopped;
inst_t::size_type progcounter;
string macro;
//MacroArg macroarg;
MacroArg macroitemlist;
MacroArgList macroarglist;
bool inmacroargs;
size_t itembegin;
size_t itemend;
AsmMachine (const AsmMachine &); // Forbidden
void operator = (const AsmMachine &); // Forbidden.
string getnocase (const string & str) const;
Token getmacroname ();
void run ();
void push (address n);
void push (const Value & v);
void push (const string & varname);
void pushliteral (const string & literal);
address popvalue ();
VarData popvar ();
address popfixvalue ();
byte popbyte ();
byte popfixbyte ();
bool popbool ();
byte popcode ();
regbCode popregb ();
regwCode popregw ();
byte popdesp ();
byte popprefix ();
TypeByteInst poptypeinst ();
TypeToken poptypetoken ();
flagCode popflag ();
flagCode popflag_val ();
void start ();
void stop ();
void opDollar ();
void doEmpty ();
void doGenLabel ();
void doAddVarItem ();
void doMacroItem ();
void doMacroArg ();
void doMacroValue ();
void doExpandMacro ();
void doASEG ();
void doCSEG ();
//void doDEFBliteral ();
void doDEFBnum ();
void doDEFBend ();
void doDEFL ();
void doDEFS ();
void doDEFSvalue ();
void doDEFWnum ();
void doDEFWend ();
void doDSEG ();
void doELSE ();
void doEND ();
void doENDn ();
void doENDIF ();
void doENDM ();
void doENDP ();
void doEQU ();
void doEXITM ();
void doEXTRN ();
void doIF ();
void doIF1 ();
void doIF2 ();
void doIFDEF ();
void doIFNDEF ();
void doINCBIN ();
void doINCLUDE ();
void doEndINCLUDE ();
void doIRP ();
void doIRPC ();
void doLOCAL ();
void doMACRO ();
void doORG ();
void doPROC ();
void doPUBLIC ();
void doREPT ();
void do_8080 ();
void do_DEPHASE ();
void do_ERROR ();
void do_PHASE ();
void do_SHIFT ();
void do_WARNING ();
void do_Z80 ();
void doNoargInst ();
void doEvalBitInst ();
void doADC_HL ();
void doADD_HL ();
void doADD_IX ();
void doADD_IY ();
void doCALL ();
void doCALL_flag ();
void doCP_r ();
void doCP_undoc ();
void doCP_indHL ();
void doCP_idesp ();
void doCP_n ();
void doIM ();
void doIN_A_indC ();
void doIN_A_indn ();
void doIN_r_indC ();
void doDJNZ ();
void doEX_indSP_HL ();
void doEX_indSP_IX ();
void doEX_indSP_IY ();
void doEX_AF_AFP ();
void doEX_DE_HL ();
void doINC_r ();
void doINC_undoc ();
void doINC_indHL ();
void doINC_idesp ();
void doINC_rr ();
void doINC_IX ();
void doINC_IY ();
void doINC_r (bool isINC, byte prefix);
void doINC_rr (bool isINC);
void doJP ();
void doJP_flag ();
void doJP_indHL ();
void doJP_indIX ();
void doJP_indIY ();
void doJR ();
void doJR_flag ();
void doLD_A_I ();
void doLD_A_R ();
void doLD_I_A ();
void doLD_R_A ();
void doLD_r_r ();
void doLD_r_undoc ();
void doLD_r_indHL ();
void doLD_r_idesp ();
void doLD_r_n ();
void doLD_undoc_r ();
void doLD_undoc_n ();
void doLD_A_r ();
void doLD_A_undoc ();
void doLD_A_n ();
void doLD_A_indexp ();
void doLD_A_indBC ();
void doLD_A_indDE ();
void doLD_A_indHL ();
void doLD_A_idesp ();
void doLD_indHL_r ();
void doLD_indHL_n ();
void doLD_indBC ();
void doLD_indDE ();
void doLD_indexp_A ();
void doLD_indexp_BC ();
void doLD_indexp_DE ();
void doLD_indexp_HL ();
void doLD_indexp_SP ();
void doLD_indexp_IX ();
void doLD_indexp_IY ();
void doLD_idesp_r ();
void doLD_idesp_n ();
void doLD_SP_HL ();
void doLD_SP_IX ();
void doLD_SP_IY ();
void doLD_SP_nn ();
void doLD_SP_indexp ();
void doLD_HL_nn ();
void doLD_HL_indexp ();
void doLD_rr_nn ();
void doLD_rr_indexp ();
void doLD_IXY_nn ();
void doLD_IXY_indexp ();
void doOUT_C_ ();
void doOUT_n_ ();
void doPUSH_rr ();
void doPUSH_IX ();
void doPUSH_IY ();
void doRET ();
void doRET_flag ();
void doRL_r ();
void doRL_undoc ();
void doRL_indhl ();
void doRL_idesp ();
void doRST ();
void doSBC_HL ();
};
AsmMachine::AsmMachine (AsmImpl & asmin_n, Tokenizer & tz_n,
ParseType pt_n, bool inpass2_n) :
asmin (asmin_n),
nocase (asmin.getnocase () ),
tz (tz_n),
pt (pt_n),
require_defined (inpass2_n),
expectingmacro (pt == PassedFirst || pt == BracketOnly),
inmacroargs (false)
{
}
Machine * newMachine (AsmImpl & asmin_n, Tokenizer & tz_n,
ParseType pt_n, bool inpass2_n)
{
return new AsmMachine (asmin_n, tz_n, pt_n, inpass2_n);
}
string AsmMachine::getnocase (const string & str) const
{
return nocase ? upper (str) : str;
}
Token AsmMachine::getmacroname ()
{
Token tok= tz.gettoken ();
if (tok.type () == TypeIdentifier)
{
const string name (getnocase (tok.str () ) );
if (asmin.ismacro (name) )
tok= Token (TypeMacroName, name);
}
return tok;
}
void AsmMachine::expectmacro ()
{
macro.erase ();
expectingmacro= true;
}
void AsmMachine::syntax (const char * /*s*/)
{
//if (! inmacroargs)
// throw logic_error (string ("Unexpectd parsing error: ") + s);
}
void AsmMachine::ugly ()
{
asmin.warningUglyInstruction ();
}
Token AsmMachine::gettoken ()
{
TRFUNC (tr, "AsmMachine::gettoken");
if (pt == PassedFirst)
{
if (expectingmacro)
{
expectingmacro= false;
return getmacroname ();
}
else
{
if (inmacroargs)
return tz.getmacroarg ();
else
return tz.gettoken ();
}
}
else
{
ParseType inipt= pt;
pt= PassedFirst;
switch (inipt)
{
case BracketOnly:
return Token (TypeToken (TOK_BracketOnly) );
default:
throw logic_error ("Invalid parse type");
}
}
}
void AsmMachine::addcode (Oper op)
{
TRFUNC (tr, "AsmMachine::addcode (op)");
inst.push_back (Instruc (op) );
}
void AsmMachine::addcode (address value)
{
TRFUNC (tr, "AsmMachine::addcode (value)");
inst.push_back (Instruc (value) );
}
void AsmMachine::addcode (const string & ident)
{
TRFUNC (tr, "AsmMachine::addcode");
TRMESSAGE (tr, ident);
inst.push_back (Instruc (ident) );
}
void AsmMachine::addcodeliteral (const string & lit)
{
TRFUNC (tr, "AsmMachine::addcodeliteral");
TRMESSAGE (tr, lit);
inst.push_back (Instruc (OpLiteral, lit) );
}
void AsmMachine::addlabel (const string & labelname)
{
TRFUNC (tr, "AsmMachine::addlabel");
ASSERT (label.empty () );
label= getnocase (labelname);
}
void AsmMachine::addmacroname (const Token & tok)
{
TRFUNC (tr, "AsmMachine::addmacroname");
const string macroname (tok.macroname () );
ASSERT (! macroname.empty () );
ASSERT (macro.empty () );
macro= getnocase (macroname);
macroargs ();
}
void AsmMachine::cancelmacroname ()
{
nomacroargs ();
ASSERT (! macro.empty () );
label= macro;
macro.erase ();
addcode (OpGenLabel);
expectmacro ();
}
void AsmMachine::redefmacro ()
{
ASSERT (! macro.empty () );
ASSERT (label.empty () );
label= macro;
macro.erase ();
nomacroargs ();
addcode (OpMACRO);
}
void AsmMachine::macroargs ()
{
TRFUNC (tr, "AsmMachine::macroargs");
inmacroargs= true;
}
void AsmMachine::nomacroargs ()
{
TRFUNC (tr, "AsmMachine::nomacroargs");
inmacroargs= false;
}
void AsmMachine::addmacroitem (const Token & tok)
{
TRFUNC (tr, "AsmMachine::addmacroitem");
TRSTREAM (tr, tok);
//macroarg.push_back (tok);
macroitemlist.push_back (tok);
addcode (OpMacroItem);
}
void AsmMachine::errmsg (const string & errmsg)
{
TRFUNC (tr, "AsmMachine::errmsg");
throw nonfatal_error (errmsg);
}
void AsmMachine::invalid ()
{
errmsg ("Invalid instruction");
}
void AsmMachine::expected (const Token & tokexp, const Token & tokfound)
{
TRF;
throw Expected (tokexp, tokfound);
}