-
Notifications
You must be signed in to change notification settings - Fork 3
/
asmimpl.cpp
5720 lines (4839 loc) · 110 KB
/
asmimpl.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
// asmimpl.cpp
// Revision 10-jan-2007
#include "asmimpl.h"
#include "token.h"
#include "parser.h"
#include "parsertypes.h"
#include "simpleinst.h"
#include "asmfile.h"
#include "var.h"
#include "codeaux.h"
#include "nullstream.h"
#include "cpc.h"
#include "tap.h"
#include "tzx.h"
#include "spectrum.h"
#include "relfile.h"
#include "segment.h"
#include "module.h"
#include "local.h"
#include "macro.h"
#include "macroframe.h"
#include "config_version.h"
#include "trace.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <stack>
#include <set>
#include <map>
#include <memory>
#include <iterator>
#include <stdexcept>
#include <algorithm>
#include <memory>
using std::ios;
using std::streambuf;
using std::streamsize;
using std::ostream;
using std::cout;
using std::cerr;
using std::endl;
using std::ifstream;
using std::ostringstream;
using std::vector;
using std::stack;
using std::set;
using std::map;
using std::make_pair;
using std::ostream_iterator;
using std::back_inserter;
using std::exception;
using std::runtime_error;
using std::logic_error;
using std::for_each;
using std::fill;
using std::copy;
using std::transform;
using std::remove_copy_if;
using std::auto_ptr;
namespace pasmo
{
namespace impl
{
// Listing auxilary constant and funcs.
const string listing_progname (PACKAGE_STRING);
const string listing_sep (" ");
string prefhex4 (address w)
{
return string (2, ' ') + hex4str (w);
}
//*********************************************************
// Exceptions.
//*********************************************************
// Errors that must never happen, they are handled for diagnose
// Pasmo bugs.
pasmo_fatal::pasmo_fatal (const std::string & s) :
logic_error (s)
{ }
logic_error UnexpectedError ("Unexpected error");
logic_error UnexpectedRegisterCode ("Unexpected register code");
logic_error InvalidRegisterUsed ("Invalid register used");
logic_error InvalidInstructionType ("Invalid instruction type");
//logic_error LocalNotExist ("Trying to use a non existent local level");
logic_error LocalNotExpected ("Unexpected local block encountered");
logic_error AutoLocalNotExpected ("Unexpected autolocal block encountered");
pasmo_fatal InvalidPassValue ("Invalid value of pass");
logic_error UnexpectedORG ("Unexpected ORG found");
logic_error UnexpectedMACRO ("Unexpected MACRO found");
logic_error MACROLostENDM ("Unexpected MACRO without ENDM");
// Errors in the code being assembled.
runtime_error ErrorReadingINCBIN ("Error reading INCBIN file");
runtime_error ErrorOutput ("Error writing object file");
runtime_error InvalidPredefine ("Can't predefine invalid identifier");
runtime_error InvalidPredefineValue ("Invalid value for predefined symbol");
runtime_error InvalidPredefineSyntax ("Syntax error in predefined symbol");
class RedefinedDEFL : public runtime_error {
public:
RedefinedDEFL (const string & varname) :
runtime_error ("Invalid definition of '" + varname +
"', previously defined as DEFL")
{ }
};
class RedefinedEQU : public runtime_error {
public:
RedefinedEQU (const string & varname) :
runtime_error ("Invalid definition of '" + varname +
"', previously defined as EQU or label")
{ }
};
class RedefinedExtern : public runtime_error {
public:
RedefinedExtern (const string & varname) :
runtime_error ("Invalid definition of '" + varname +
"', previously defined as extern")
{ }
};
runtime_error InvalidInAutolocal ("Invalid use of name in autolocal mode");
runtime_error InvalidSharpSharp ("Invalid use of ##");
runtime_error IFwithoutENDIF ("IF without ENDIF");
runtime_error ELSEwithoutIF ("ELSE without IF");
runtime_error ELSEwithoutENDIF ("ELSE without ENDIF");
runtime_error ENDIFwithoutIF ("ENDIF without IF");
runtime_error UnbalancedPROC ("Unbalanced PROC");
runtime_error UnbalancedENDP ("Unbalanced ENDP");
runtime_error MACROwithoutENDM ("MACRO without ENDM");
runtime_error REPTwithoutENDM ("REPT without ENDM");
runtime_error ENDMwithoutMacro ("ENDM without corresponding macro directive");
runtime_error IRPwithoutENDM ("IRP without ENDM");
runtime_error ENDMOutOfMacro ("ENDM outside of macro");
pasmo_fatal ShiftOutsideMacro (".SHIFT outside MACRO");
runtime_error InvalidBaseValue ("Invalid base value");
runtime_error ParenInsteadOfBracket ("Expected ] but ) found");
runtime_error BracketInsteadOfParen ("Expected ) but ] found");
runtime_error OffsetOutOfRange ("Offset out of range");
runtime_error RelativeOutOfRange ("Relative jump out of range");
runtime_error BitOutOfRange ("Bit position out of range");
runtime_error InvalidInstruction ("Invalid instruction");
runtime_error InvalidFlagJR ("Invalid flag for JR");
runtime_error InvalidValueRST ("Invalid RST value");
runtime_error InvalidValueIM ("Invalid IM value");
runtime_error NotValid86 ("Instruction not valid in 86 mode");
runtime_error IsPredefined ("Can't redefine, is predefined");
runtime_error OutOfSyncPRL ("PRL generation failed: out of sync");
runtime_error OutOfSyncREL ("REL generation failed: out of sync");
class NoInstruction : public runtime_error {
public:
NoInstruction (const Token & tok) :
runtime_error ("Unexpected '" + tok.str () +
"' used as instruction")
{ }
};
class UndefinedVar : public runtime_error {
public:
UndefinedVar (const string & varname) :
runtime_error ("Symbol '" + varname + "' is undefined")
{ }
UndefinedVar (const VarData & vd) :
runtime_error ("Symbol '" + vd.getname () + "' is undefined")
{ }
};
class Expected : public runtime_error {
public:
Expected (const Token & tokexp, const Token & tokfound) :
runtime_error ("'" + tokexp.str () + "' expected but '" +
tokfound.str () + "' found")
{ }
Expected (const string & expected, const Token & tok) :
runtime_error (expected + " expected but '" +
tok.str () + "' found")
{ }
Expected (const string & expected, const string & found) :
runtime_error (expected + " expected but '" +
found + "' found")
{ }
};
class EndLineExpected : public Expected {
public:
EndLineExpected (const Token & tok) :
Expected ("End line", tok)
{ }
};
class IdentifierExpected : public Expected {
public:
IdentifierExpected (const Token & tok) :
Expected ("Identifier", tok)
{ }
};
class MacroExpected : public Expected {
public:
MacroExpected (const string & name) :
Expected ("Macro name", name)
{ }
};
class ValueExpected : public Expected {
public:
ValueExpected (const Token & tok) :
Expected ("Value", tok)
{ }
};
class SomeOpenExpected : public Expected {
public:
SomeOpenExpected (const Token & tok) :
Expected ("( or [", tok)
{ }
};
class TokenExpected : public Expected {
public:
TokenExpected (TypeToken ttexpect, const Token & tokfound) :
Expected (gettokenname (ttexpect), tokfound)
{ }
};
class OffsetExpected : public Expected {
public:
OffsetExpected (const Token & tok) :
Expected ("Offset expression", tok)
{ }
};
class ErrorDirective : public runtime_error {
public:
ErrorDirective (const Token & tok) :
runtime_error (".ERROR directive: " + tok.str () )
{ }
ErrorDirective (const string & msg) :
runtime_error (".ERROR directive: " + msg)
{ }
};
class UndefinedInPass1 : public runtime_error {
public:
UndefinedInPass1 (const string & name) :
runtime_error ("The symbol '" + name +
"' must be defined in pass 1")
{ }
};
void checktoken (TypeToken ttexpected, const Token & tok)
{
if (tok.type () != ttexpected)
throw TokenExpected (ttexpected, tok);
}
void checkidentifier (const Token & tok)
{
checktoken (TypeIdentifier, tok);
}
//*********************************************************
// Auxiliary functions and constants.
//*********************************************************
const string emptystr;
const string openIndir ("[");
const string closeIndir ("]");
const bool nameSP= true;
const bool nameAF= false;
string incordec (bool isINC)
{
return string (isINC ? "INC " : "DEC ");
}
string inrordcr (bool isINC)
{
return string (isINC ? "INR " : "DCR ");
}
string inxordcx (bool isINC)
{
return string (isINC ? "INX " : "DCX ");
}
string tablabel (string str)
{
const string::size_type l= str.size ();
if (l < 8)
str+= "\t\t";
else
if (l < 16)
str+= '\t';
else
str+= ' ';
return str;
}
bool ismacrodirective (TypeToken tt)
{
return tt == TypeMACRO || tt == TypeREPT ||
tt == TypeIRP || tt == TypeIRPC;
}
void putvarnamelist (ostream & out, const VarnameList & varnamelist)
{
ASSERT (! varnamelist.empty () );
//VarnameList::const_iterator last= --varnamelist.end ();
VarnameList::const_iterator last= varnamelist.end ();
--last;
copy (varnamelist.begin (), last,
ostream_iterator <string> (out, ", ") );
out << * last << endl;
}
//*********************************************************
// class AsmReal
//*********************************************************
class AsmReal : public AsmImpl, public AsmFile,
public Vars, public MacroStore
{
public:
AsmReal (const AsmOptions & options_n);
// This is not a copy constructor, it creates a new
// instance copying the options and the AsmFile.
explicit AsmReal (const AsmReal & in);
~AsmReal ();
static Asm * create (const AsmOptions & options_n);
ValueType getinitialsegment ();
void setbase (unsigned int addr);
void addincludedir (const string & dirname);
void addpredef (const string & predef);
void setfilelisting (ostream & out_n);
AsmMode getasmmode () const;
bool getnocase () const;
void loadfile (const string & filename);
void link_modules (vector <Module *> & vpmod);
void loadmodules (vector <Module> & mod);
void link ();
void processfile ();
int currentpass () const;
//address getcurrentinstruction () const;
Value getcurrentinstruction () const;
// Variable access for local classes.
VarData getvar (const string & varname);
VarData rawgetvar (const string & varname);
string genlocalname (const string & varname);
// Object file generation.
address getminused () const;
address getmaxused () const;
size_t getcodesize () const;
void message_emit (const string & type);
void writebincode (ostream & out);
void emitobject (ostream & out);
void emitdump (std::ostream & out);
void emitplus3dos (ostream & out);
void emittap (ostream & out);
void writetzxcode (ostream & out);
void emittzx (ostream & out);
void writecdtcode (ostream & out);
void emitcdt (ostream & out);
string cpcbasicloader ();
void emitcdtbas (ostream & out);
string spectrumbasicloader ();
void emittapbas (ostream & out);
void emittzxbas (ostream & out);
void emithex (ostream & out);
void emitamsdos (ostream & out);
void emitprl (ostream & out);
void emitrel (ostream & out);
void emitcmd (ostream & out);
void emitcom (ostream & out);
void emitmsx (ostream & out);
void emitcode (ostream & out);
void dumppublic (ostream & out);
void dumpsymbol (ostream & out);
private:
void operator = (const AsmReal &); // Forbidden.
// Bug reported by Mauri
// Extra calification fails is some compilers.
//static streambuf * AsmReal::pnullbuf ();
static streambuf * pnullbuf ();
void setentrypoint (address addr);
void checkendline (const Token & tok);
void checkendline (Tokenizer & tz);
//address currentpos () const;
Value currentpos () const;
void clearphase ();
//address phased (address addr) const;
//address phasedpos () const;
Value phased (Value addr) const;
Value phasedpos () const;
void genbyte (byte abyte);
void genword (address dataword);
void gencode (byte code);
void gencode (const VarData & vd);
void gencode (byte code1, byte code2);
void gencode (byte code1, byte code2, byte code3);
void gencode (byte code1, byte code2, byte code3, byte code4);
void gencodeED (byte code);
void gencodeword (address value);
void gencodeword (const Value & v);
void gencodeword (const VarData & vd);
void showcode (const string & instruction);
void showdebnocodeline (const Tokenizer & tz);
public:
void warningUglyInstruction ();
void parse_error (const string & errmsg);
void doEmpty ();
void doLabel (const string & varname);
void doExpandMacro (const string & name,
const MacroArgList & params);
void doASEG ();
void doCSEG ();
void doDEFBliteral (const string & s);
void doDEFBnum (byte b);
//void doDEFBnum (const VarData & vd);
void doDEFBend ();
void doDEFL (const string & label, address value);
void doDEFS (address count, byte value);
//void doDEFWnum (address num);
void doDEFWnum (const VarData & num);
void doDEFWend ();
void doDSEG ();
void doELSE ();
//void doEND (address end, bool hasentry);
void doEND ();
void doEND (const VarData & vd);
void doENDIF ();
void doENDM ();
void doENDP ();
void doEQU (const string & label, const VarData & vdata);
void doEXITM ();
void doEXTRN (const VarnameList & varnamelist);
void doIF (address v);
void doIF1 ();
void doIF2 ();
void doIFDEF (const string & varname);
void doIFNDEF (const string & varname);
void doINCBIN (const string & includefile);
void doINCLUDE ();
void doEndOfINCLUDE ();
void doIRP (const string & varname, const MacroArgList & params);
void doIRPC (const string & varname, const string & charlist);
void doLOCAL (const VarnameList & varnamelist);
void doMACRO (const string & name, const vector <string> & param);
void doPROC ();
void doORG (address neworg);
void doPUBLIC (const VarnameList & varnamelist);
void doREPT (address counter, const string & varcounter,
address valuecounter, address step);
void do_8080 ();
void do_DEPHASE ();
void do_ERROR (const string & msg);
void do_PHASE (address value);
void do_SHIFT ();
void do_WARNING (const string & msg);
void do_Z80 ();
void doByteInst (TypeByteInst ti, regbCode reg,
byte prefix= prefixNone, bool hasdesp= false, byte desp= 0);
void doByteInmediate (TypeByteInst ti, byte bvalue);
void doByteInstCB (byte codereg, regbCode reg,
byte prefix= prefixNone, bool hasdesp= false, byte desp= 0);
void doNoargInst (TypeToken tt);
void doADDADCSBC_HL (byte basecode, regwCode reg, byte prefix);
void doDJNZ (const VarData & vd);
//void doCALL (address addr);
void doCALL (const VarData & addr);
//void doCALL_flag (flagCode fcode, address addr);
void doCALL_flag (flagCode fcode, const VarData & vd);
void doEX_indSP_HL ();
void doEX_indSP_IX ();
void doEX_indSP_IY ();
void doEX_AF_AFP ();
void doEX_DE_HL ();
void doIN_A_indC ();
void doIN_A_indn (byte n);
void doINr_c_ (regbCode reg);
void doIM (address v);
//void doJP (address addr);
void doJP (const VarData & addr);
void doJP_indHL ();
void doJP_indIX ();
void doJP_indIY ();
//void doJP_flag (flagCode fcode, address addr);
void doJP_flag (flagCode fcode, const VarData & addr);
void doRelative (byte code, address addr, const string instrname);
void doRelative (byte code, const VarData & vd,
const string instrname);
void doJR (const VarData & vd);
void doJR_flag (flagCode fcode, const VarData & vd);
void doLDir (byte type);
void doLD_r_r (regbCode reg1, regbCode reg2);
void doLD_r_n (regbCode reg, byte n);
void doLD_r_undoc (regbCode reg1, regbCode reg2, byte prefix);
void doLD_r_idesp (regbCode reg1, byte prefix, byte desp);
void doLD_undoc_r (regbCode reg1, byte prefix, regbCode reg2);
void doLD_undoc_n (regbCode reg, byte prefix, byte n);
void doLD_idesp_r (byte prefix, byte desp, regbCode reg2);
void doLD_A_indexp (const VarData & vd);
void doLD_A_indBC ();
void doLD_A_indDE ();
void doLD_indBC_A ();
void doLD_indDE_A ();
void doLD_indexp_A (const VarData & vd);
void doLD_indexp_BC (const VarData & vd);
void doLD_indexp_DE (const VarData & vd);
void doLD_indexp_HL (const VarData & vd);
void doLD_indexp_SP (const VarData & vd);
void doLD_indexp_IX (const VarData & vd);
void doLD_indexp_IY (const VarData & vd);
void doLD_idesp_n (byte prefix, byte desp, byte n);
void doPUSHPOP (regwCode reg, byte prefix, bool isPUSH);
void doLD_SP_HL ();
void doLD_SP_IX ();
void doLD_SP_IY ();
void doLD_SP_nn (const VarData & value);
void doLD_SP_indexp (const VarData & value);
void doLD_HL_nn (const VarData & value);
void doLD_HL_indexp (const VarData & vd);
//void doLD_rr_nn (regwCode regcode, byte prefix, address value);
void doLD_rr_nn (regwCode regcode,
const VarData & addr);
//void doLD_rr_indexp (regwCode regcode, byte prefix, address value);
void doLD_rr_indexp (regwCode regcode,
const VarData & addr);
void doLD_IXY_nn (byte prefix, const VarData & addr);
void doLD_IXY_indexp (byte prefix, const VarData & addr);
void doINC_r (bool isINC, byte prefix, regbCode reg);
void doINC_IX (bool isINC, address adesp);
void doINC_IY (bool isINC, address adesp);
void doINC_rr (bool isINC, regwCode reg, byte prefix);
void doOUT_C_ (regbCode rcode);
void doOUT_n_ (byte b);
void doRET ();
void doRETflag (flagCode fcode);
void doRST (address addr);
private:
void showlistingblank (const string & txt);
void showlisting ();
void showlistingsymbols ();
void showlistingequ (address value);
void showlistingheader ();
string getlistingstatus () const;
string getcurrentlistingtext ();
address getvalue (const string & var, bool required, bool ignored);
bool setvardef (const string & varname,
address value, Defined defined);
bool setvardef (const string & varname,
const VarData & vdata, Defined defined);
public:
bool isdefined (const string & varname);
private:
Tokenizer getcurrenttz ();
void parseinstruction (Tokenizer & tz);
void do_iftrue (TypeToken ttif);
void do_iffalse (TypeToken ttif);
void do_if (TypeToken ttif, bool valueif);
void parseline (Tokenizer & tz);
//void link_rel_module (const string & relname);
//void link_modules ();
void dopass ();
bool setequorlabel (const string & varname, address value);
bool setlabel (const string & varname, const Value & v);
bool setequ (const string & varname, const VarData & vdata);
bool setdefl (const string & varname, address value);
// Aux error and warning functions.
void emitwarning (const string & text);
void no8080 ();
void no86 ();
// Z80 instructions.
//void genCALL (byte code, address addr);
void genCALL (byte code, const VarData & addr);
//void genJP (byte code, address addr);
void genJP (byte code, const VarData & addr);
// Variables.
const AsmOptions opt;
AsmMode asmmode;
GenCodeMode genmode;
// ********* Information streams ********
bool debout_flag;
bool listing_file;
bool listing_flag;
ostream debout;
ostream errout;
ostream verbout;
ostream warnout;
ostream listout;
size_t counterr;
Module mainmodule;
Segment mainseg;
address link_base;
address base;
//address current;
bool phase_active;
address phasing;
//address currentinstruction;
Value currentinstruction;
//address minused;
//address maxused;
address entrypoint;
bool hasentrypoint;
int pass;
bool end_reached;
vector <size_t> ifline;
size_t iflevel;
public:
size_t getiflevel () const;
void setiflevel (size_t newlevel);
void deciflevel ();
private:
size_t includelevel;
size_t macrolevel;
int listingpagelen;
int listinglines;
int listingspage;
int listingstep;
vector <string> hexlisting;
// ********* Local **********
size_t localcount;
vector <string> localnames;
void initlocal ();
LocalStack localstack;
public:
void pushlocal (LocalLevel * plevel);
LocalLevel * toplocal () const;
void poplocal ();
private:
bool isautolocalname (const string & varname);
AutoLevel * enterautolocal ();
void finishautolocal ();
void checkautolocal (const string & varname);
void verifynoautolocal (const string & varname);
void enterorfinishautolocal (const string & varname);
void checkafterprocess ();
// ********* Macro **********
//MapMacro mapmacro;
public:
const Macro & getmacro (const string & name);
bool ismacro (const string & name) const;
private:
bool gotoENDM ();
void domacroexpansion (MacroFrameMacro & mframe);
void expandIRP (MacroFrameIRPbase & macroirp,
const MacroArgList & params);
MacroFrameBase * pcurrentmframe;
public:
MacroFrameBase * getmframe () const;
void setmframe (MacroFrameBase * pnew);
// ********** Extern references **********
private:
typedef map <address, string> ChainExtern;
ChainExtern chainextern;
typedef map <address, address> ExternOffset;
ExternOffset externoffset;
public:
typedef map <address, Value> Relative;
//typedef map <address, string> RefToExtern;
typedef map <string, address> RefToExtern;
typedef map <address, Value> Offset;
private:
Relative relative;
RefToExtern reftoextern;
Offset offset;
};
//*********************************************************
// class AsmImpl definitions
//*********************************************************
AsmImpl::AsmImpl ()
{ }
AsmImpl::AsmImpl (const AsmImpl & in) :
Asm (in)
{ }
AsmImpl::~AsmImpl ()
{ }
Asm * AsmImpl::create (const AsmOptions & options_n)
{
return new AsmReal (options_n);
}
//*********************************************************
// class AsmReal definitions
//*********************************************************
AsmReal::AsmReal (const AsmOptions & options_n) :
AsmImpl (),
AsmFile (),
opt (options_n),
asmmode (opt.asmmode),
genmode (opt.mode86 ? gen86 : gen80),
debout_flag (true),
listing_file (false),
listing_flag (false),
debout (pnullbuf () ),
errout (opt.redirecterr ? cout.rdbuf () : cerr.rdbuf () ),
verbout (opt.verbose ? cerr.rdbuf () : pnullbuf () ),
warnout (cerr.rdbuf () ),
listout (pnullbuf () ),
mainmodule (debout, warnout),
//link_base ( (opt.getObjectType () == ObjectCom) ? 0x100 : 0),
link_base (opt.getLinkBase () ),
base (link_base),
//current (base),
phase_active (false),
phasing (0),
currentinstruction (ValueAbsolute, 0),
//minused (65535),
//maxused (0),
hasentrypoint (false),
pass (0),
end_reached (false),
localcount (0),
pcurrentmframe (0)
{
}
AsmReal::AsmReal (const AsmReal & in) :
AsmImpl (in),
AsmFile (in),
//Vars (in),
Vars (),
MacroStore (),
opt (in.opt),
asmmode (AsmZ80),
genmode (in.genmode),
debout_flag (true),
listing_file (false),
listing_flag (false),
debout (pnullbuf () ),
errout (in.errout.rdbuf () ),
verbout (in.verbout.rdbuf () ),
warnout (in.warnout.rdbuf () ),
listout (pnullbuf () ),
mainmodule (debout, warnout),
link_base (in.link_base),
base (link_base),
//current (base),
phase_active (false),
phasing (0),
currentinstruction (ValueAbsolute, 0),
//minused (65535),
//maxused (0),
hasentrypoint (false),
localcount (0),
pcurrentmframe (0)
{
}
AsmReal::~AsmReal ()
{
TRFUNC (tr, "AsmReal::~AsmReal");
}
Asm * AsmReal::create (const AsmOptions & options_n)
{
return new AsmReal (options_n);
}
ValueType AsmReal::getinitialsegment ()
{
const ObjectType otype (opt.getObjectType () );
if (otype == ObjectRel || otype == ObjectPrl ||
otype == ObjectCom || otype == ObjectCmd)
{
return ValueProgRelative;
}
else
{
return ValueAbsolute;
}
}
void AsmReal::setfilelisting (ostream & out_n)
{
listing_file= true;
listout.rdbuf (out_n.rdbuf () ),
listingpagelen= 56;
listinglines = listingpagelen;
listingspage = 0;
listingstep = 1;
listing_flag= true;
showlistingheader ();
listing_flag= false;
}
void AsmReal::setbase (unsigned int addr)
{
if (addr > 65535)
throw InvalidBaseValue;
base= static_cast <address> (addr) + link_base;
verbout << "Setting base to " << base << endl;
//current= base;
//phasing= 0;
clearphase ();
//currentinstruction= base;
}
AsmMode AsmReal::getasmmode () const
{
return asmmode;
}
bool AsmReal::getnocase () const
{
return opt.nocase;
}
void AsmReal::addincludedir (const string & dirname)
{
AsmFile::addincludedir (dirname);
}
void AsmReal::addpredef (const string & predef)
{
// Default value.
address value= 0xFFFF;
// Prepare the parsing of the argument.
Tokenizer trdef (predef, AsmZ80);
// Get symbol name.
Token tr (trdef.gettoken () );
if (tr.type () != TypeIdentifier)
throw InvalidPredefine;
string varname= tr.str ();
// Get the value, if any.
tr= trdef.gettoken ();
switch (tr.type () )
{
case TypeEqOp:
tr= trdef.gettoken ();
if (tr.type () != TypeNumber)
throw InvalidPredefineValue;
value= tr.num ();
tr= trdef.gettoken ();
if (tr.type () != TypeEndLine)
throw InvalidPredefineValue;
break;
case TypeEndLine:
break;
default:
throw InvalidPredefineSyntax;
}