1 /*------------------------------------------------------------------*/
2 /*								    */
3 /*			MC68000 Cross Assembler			    */
4 /*								    */
5 /*		  Copyright 1985 by Brian R. Anderson		    */
6 /*								    */
7 /*                 Global variables - April 16, 1991		    */
8 /*								    */
9 /*   This program may be copied for personal, non-commercial use    */
10 /*   only, provided that the above copyright notice is included	    */
11 /*   on all copies of the source code.  Copying for any other use   */
12 /*   without the consent of the author is prohibited.		    */
13 /*								    */
14 /*------------------------------------------------------------------*/
15 /*								    */
16 /*		Originally published (in Modula-2) in		    */
17 /*	    Dr. Dobb's Journal, April, May, and June 1986.	    */
18 /*								    */
19 /*	 AmigaDOS conversion copyright 1991 by Charlie Gibbs.	    */
20 /*								    */
21 /*------------------------------------------------------------------*/
22 
23 #ifdef PRIMARY
24 #define GLOBAL
25 #else
26 #define GLOBAL extern
27 #endif
28 
29 GLOBAL char SourceFN[MAXFN];	/* Source file name */
30 GLOBAL char HeaderFN[MAXFN];	/* Header file name (-h) */
31 GLOBAL char SrecFN[MAXFN];	/* Object file name (-o) */
32 GLOBAL char InclList[MAXLINE];	/* List of directories to search (-i) */
33 GLOBAL char IdntName[MAXLINE];	/* Program unit name */
34 
35 struct fs {
36     int  fd;		/* File handle */
37     char *Buf;		/* Pointer to buffer */
38     char *Ptr;		/* Current position in buffer */
39     char *Lim;		/* Logical end of buffer */
40 };
41 GLOBAL struct fs In;	/* Input file */
42 GLOBAL struct fs Eq;	/* Equate file */
43 GLOBAL struct fs List;	/* Listing file */
44 GLOBAL struct fs Srec;	/* Object file */
45 
46 /*	Command-line options	*/
47 
48 GLOBAL int AllRelocs;	/* -a Output all relocs. - added by Kevin Kofler in
49                      	      v.2.71.F3l */
50 GLOBAL int DumpSym;	/* -d Dump the symbol table. */
51 GLOBAL char DumpSymList[MAXLINE];	/* -d selection list */
52 GLOBAL int FwdProc;	/* -f Special processing for forward references */
53 GLOBAL int GlobalXREF;	/* -g Make all unknown globals XREFed */
54 GLOBAL int SuppList;	/* (neither -l nor -x) Suppress listing file. */
55 GLOBAL long DataOffset;	/* -m Offset to small data base (normally 32767) */
56 GLOBAL int NoOpt;	/* -n Suppress all optimization. */
57 GLOBAL int LnMax;	/* -p Maximum number of lines per page */
58 GLOBAL int Quiet;	/* -q Line no. display interval (0 to suppress) */
59 GLOBAL long optim;	/* -r Remove optimization mask - added by Paul Froissart
60                                                      in v.2.71.F3c */
61 GLOBAL int KeepTabs;	/* -t Keep tabs in the listing file. */
62 GLOBAL int Unaligned;	/* -u Allow unaligned DC.W, DC.L, DCB.W, DCB.L, DS.W,
63                      	      DS.L and code. - added by Kevin Kofler in
64                      	      v.2.71.F3k */
65 GLOBAL long HashSize;	/* -w Number of entries in the hash table */
66 GLOBAL int XrefList;	/* -x Produce a cross-reference listing. */
67 GLOBAL int HashStats;	/* -y Display hashing statistics. */
68 GLOBAL int DebugStart;	/* -z Debug display starts here. */
69 GLOBAL int DebugEnd;	/* -z Debug display ends here. */
70 
71 GLOBAL char TTLstring[MAXLINE];	/* Title string */
72 
73 GLOBAL int  LabLine;		/* Last labeled line number */
74 GLOBAL int  LineCount;		/* Source line counter */
75 GLOBAL char Line[MAXLINE];	/* Current source line */
76 GLOBAL char Label[MAXLINE];	/* Instruction label */
77 GLOBAL char OpCode[MAXLINE];	/* Instruction mnemonic */
78 GLOBAL char SrcOp[MAXLINE];	/* First (source) operand */
79 GLOBAL char DestOp[MAXLINE];	/* Second (destination) operand */
80 GLOBAL int  LabLoc, OpLoc;	/* Label and mnemonic start here */
81 GLOBAL int  SrcLoc, DestLoc;	/* Operands start here. */
82 GLOBAL int  Dir, PrevDir;	/* Assembler directive */
83 GLOBAL int  NumSyms;		/* Number of symbols */
84 GLOBAL long ObjOp;		/* OpCode object code */
85 GLOBAL long ObjSrc;		/* Source operand object code */
86 GLOBAL long ObjDest;		/* Destination operand object code */
87 GLOBAL char ObjString[MAXLINE];	/* String data */
88 GLOBAL int  nO, nS, nD, nX;	/* Length of above components */
89 GLOBAL int  PrntAddr;		/* Print AddrCnt on listing. */
90 GLOBAL int  MakeHunk;		/* We must make a hunk. */
91 GLOBAL int  ListOff;		/* NOLIST is supressing listing lines. */
92 GLOBAL int  LnCnt;		/* Number of lines on current page */
93 GLOBAL int  PgCnt;		/* Page number */
94 GLOBAL long Hunk2;		/* Hunk number (from GetValue) */
95 GLOBAL int  DefLine2;		/* Definition line number */
96 GLOBAL int  SingleFlag;		/* Expression consists of a single term. */
97 GLOBAL int  GotEqur;		/* We have register equates. */
98 GLOBAL int  SmallData;		/* Register for small data model (or -1) */
99 GLOBAL int  AnyNear;		/* We got at least one NEAR directive. */
100 GLOBAL int  FwdShort;		/* Forward reference could be made short. */
101 
102 GLOBAL int  Pass2;	/* Pass 2 flag */
103 GLOBAL long AddrCnt;	/* Location counter */
104 GLOBAL long AddrAdv;	/* Bump AddrCnt by this much. */
105 GLOBAL long DupFact;	/* Duplication factor for DCB, 1 otherwise */
106 GLOBAL long OrgHigh;	/* Highest address reached if we ORG backwards */
107 GLOBAL long OrgSeek;	/* Return here in Srec if we ORG backwards. */
108 GLOBAL long EndAddr;	/* END statement transfer address */
109 GLOBAL long SectStart;	/* Current section (or portion) starts here. */
110 GLOBAL int  SectLine;	/* Line number where section started */
111 GLOBAL int  HunkSeq;	/* Hunk sequence number */
112 GLOBAL long HunkType;	/* Current hunk type */
113 GLOBAL long HunkFlags;	/* Hunk flags (MEMF_FAST or MEMF_CHIP) */
114 GLOBAL long CurrHunk;	/* Current hunk number */
115 GLOBAL long NextHunk;	/* Next available hunk number */
116 GLOBAL long LenPos;	/* Seek position of current hunk length */
117 GLOBAL char *LenPtr;	/* Pointer to length if in buffer, else NULL */
118 GLOBAL int  InclErrs;	/* Error processing INCLUDE statement(s) */
119 GLOBAL int  InnrFMac;	/* An inner file has been read by a user macro. */
120 GLOBAL int  OrgFlag;	/* ORG may require object file fixup. */
121 
122 GLOBAL int  SFormat;	/* Generate S-record format. */
123 GLOBAL long StartAddr;	/* Address that record starts on */
124 GLOBAL long TempAddr;	/* Address of where we are now */
125 
126 GLOBAL int  IncStart;	/* Start line number of skippable INCLUDE */
127 GLOBAL struct InFCtl *IncPtr;	/* Copy of InF for skippable INCLUDE */
128 struct SkipEnt {		/* Skippable INCLUDE description */
129     struct SetFixup *Set1;	/*  Pointer to first SET fixup */
130     int Start;			/*  Starting line number of INCLUDE */
131     int Finish;			/*  Ending line number of INCLUDE */
132     int MCount;			/*  Value of MacCount at end of INCLUDE */
133     int LLine;			/*  Value of LabLine at end of INCLUDE
134 						    -- added by Kevin Kofler in v.2.71.F3i */
135 };
136 struct SetFixup {		/* SET symbol fixup entry */
137     struct SymTab *Sym;		/* Pointer to symbol table entry */
138     long Val;			/* Fixup value */
139     long Hunk;			/* Fixup hunk number */
140 };
141 GLOBAL struct SkipEnt *SkipLim;	/* Logical end of skippable INCLUDEs */
142 GLOBAL struct SkipEnt *SkipIdx;	/* Current skippable INCLUDE entry */
143 GLOBAL struct SetFixup *SetFixLim;	/* Next available SetFixup */
144 
145 struct SymTab {				/* Symbol table */
146     struct SymTab *Link;	/* Link to next entry in hash chain */
147     char *Nam;	/* Pointer to symbol */
148     long Val;	/* Value */
149     long Hunk;	/* Hunk number (ORed with MEMF_CHIP or MEM_FAST
150 			if applicable SECTION
151 		   ~(pointer to symbol) if XREF
152 		   Pointer to macro text if MACRO		*/
153     int  Defn;	/* Line number where defined */
154     int  Flags;	/* Flags bits:	0 - XREF
155 				1 - XDEF
156 				2 - SET
157 				3 - MACRO (symbol is preceded by blank)
158 				4 - SECTION (name preceded by 2 blanks
159 					and 4-digit hex sequence number)
160 				5 - register name (EQUR)
161 				6 - register list (REG)
162 				7 - PUBLIC (XREF or XDEF will be set)
163 				8 - An EQU that doesn't yet know whether
164 				    it is an XREF, an XDEF, or just a
165 				    plain forward reference */
166     struct Ref *Ref1;	/* Pointer to first reference entry */
167 };
168 GLOBAL struct SymTab *SymStart;	/* The symbol table starts here. */
169 GLOBAL struct SymTab *SymLim;	/* The symbol table ends here. */
170 GLOBAL struct SymTab *SymCurr;	/* Start of current chunk of data */
171 GLOBAL struct SymTab *Sym;	/* ReadSymTab sets this up. */
172 GLOBAL struct SymTab *Sect;	/* Current section's entry */
173 GLOBAL struct SymTab *SymChunk;	/* Current symbol chunk for NextSym */
174 GLOBAL struct SymTab *SymChLim;	/* End of *SymChunk chunk */
175 GLOBAL struct SymTab **Hash;	/* Pointer to hash table */
176 GLOBAL struct SymTab **SymSort;	/* Symbol sort area */
177 GLOBAL int  *HashCount;		/* Hashing summary table */
178 
179 struct NameChunk {		/* Chunk of labels or macro text */
180     struct NameChunk *Link;	/* Link to the next chunk */
181     char *Data[CHUNKSIZE-sizeof(struct NameChunk *)];	/* Data area */
182 };
183 GLOBAL struct NameChunk *NameStart;	/* Start of first name chunk */
184 GLOBAL char             *NameLim;	/* Next available name entry */
185 GLOBAL struct NameChunk *NameCurr;	/* Start of current name chunk */
186 
187 struct Ref {			/* Reference entry */
188     struct Ref *NextRef;	/* Pointer to next reference entry */
189     int RefNum[MAXREF];		/* Reference line numbers */
190 };
191 GLOBAL struct Ref *RefLim;	/* Next available reference entry */
192 
193 struct RelTab {			/* Relocation table entry */
194     struct RelTab *Link;	/* Link to the next entry */
195     long Offset;		/* Offset to relocatable value */
196     long Hunk;			/* Hunk type to relocate relative to */
197     int  Size;			/* Size of relocatable value */
198     int  IsPC;
199 /* added by Kevin Kofler in v.2.71.F3l: */
200     long Target;  /* Reloc target. -1 if stored at Offset. */
201     int Negative; /* Reloc is negative. */
202 };
203 GLOBAL struct RelTab *RelStart;	/* Relocation data starts here */
204 GLOBAL struct RelTab *RelLim;	/* Relocation data ends here */
205 GLOBAL struct RelTab *RelCurr;	/* Start of current chunk of data */
206 GLOBAL struct RelTab *RelLast;	/* Last relocation entry added */
207 
208 struct TermStack {		/* Parser's term stack */
209     long value;			/* Value */
210     long hunk;			/* Hunk number */
211     int  oploc;			/* Location in source statement */
212     int  defline;		/* Line number where defined */
213     int  isaddrdiff;	/* term is an address difference
214                     	   added by Kevin Kofler in v.2.71.F3l */
215 };
216 GLOBAL struct TermStack *Term;	/* Term stack pointer */
217 
218 struct OpStack {		/* Parser's operator stack */
219     char chr;			/* Operator character */
220     int  prec;			/* Precedence */
221 };
222 GLOBAL struct OpStack *Ops;	/* Operator stack pointer */
223 
224 GLOBAL char OpPrec[256];	/* Operator precedence look-up table */
225 
226 GLOBAL int  InFNum;		/* Current input nesting level */
227 struct InFCtl {
228     long Pos;			/* Current position in input */
229     char *UPtr;			/* Current position in user macro or 0 */
230     char *NPtr;			/* File name stack pointer */
231     int  Line;			/* Current line number in this file */
232     int  NArg;			/* Number of macro arguments or -1 */
233     int  MCnt;			/* Macro expansion number (for \@) */
234 };
235 GLOBAL struct InFCtl *InF;	/* Macro/include file stack pointer */
236 GLOBAL struct InFCtl *LowInF;	/* "Low-water mark" for InF */
237 GLOBAL char *Heap2;		/* Secondary heap */
238 GLOBAL char *NextFNS;		/* Next input file path/name */
239 GLOBAL char *High2;		/* Secondary high-water mark */
240 GLOBAL char *Low2;		/* Low limit from top of heap */
241 
242 GLOBAL int  OuterMac;		/* Level number of outermost macro */
243 GLOBAL int  MacCount;		/* Number of macros expanded */
244 GLOBAL int  SkipNest;		/* Skipped IF/ENDC nesting count */
245 GLOBAL char MacSize[2];		/* Macro call size ("B", "W", or "L") */
246 
247 struct OpConfig {		/* Operand configuration */
248     long Value;	/* Value */
249     long Hunk;	/* Hunk number */
250     int  Defn;	/* Line number where defined */
251     int  Mode;	/* Addressing mode */
252     int  Loc;	/* Location of operand on Line */
253     int  Rn;	/* Register number */
254     int  Xn;	/* Index register number */
255     int  Xsize;	/* Size of index */
256     int  X;	/* Is index Data or Address reg? */
257     int  Single; /* The operand is a single term. */
258 /* added by Kevin Kofler in v.2.71.F3o: */
259     int  PCConv;  /* Adjustment for PC-relative operand */
260 /* added by Kevin Kofler in v.2.71.F3t: */
261     int  Unopt;   /* Unoptimizable operand (hardcoded size) */
262 };
263 GLOBAL struct OpConfig Src, Dest;	/* Source and destination operands */
264 
265 GLOBAL int Size;	/* Size for OpCode */
266 GLOBAL int InstSize;	/* Size of instruction, including operands */
267 GLOBAL int AdrModeA;	/* Addressing modes for this instruction */
268 GLOBAL int AdrModeB;	/*                 ditto                 */
269 GLOBAL int Op;		/* Raw bit pattern for OpCode */
270 
271 GLOBAL struct FwdBr {	/* Forward branch optimization candidates */
272     long Loc;		/* Location of Bcc instruction */
273     struct SymTab *FwdSym;	/* Pointer to target label entry */
274     int  Line;		/* Line number (copied to FwdTable if OK) */
275 } FwdBranch[64];
276 GLOBAL struct FwdBr *FwdLim1;	/* Logical end of FwdBranch */
277 GLOBAL struct SymTab *FwdBranchFix[2048]; /* Symbol table entries to fix */
278 /* This was: GLOBAL struct SymTab *FwdBranchFix[128];
279    However, that won't work if one puts lots of labels at the same place.
280    This change allows to assemble PedroM correctly.
281    -- Kevin Kofler, v.2.71.F3k-pre1 */
282 GLOBAL struct SymTab **FwdFixLimit;	/* Logical end of fixup table */
283 
284 struct FwdTable {
285     struct FwdTable *Link;
286     int FwdLine[(FWDSIZE-sizeof(struct FwdTable *)) / sizeof (int)];
287 };
288 GLOBAL struct FwdTable *FwdStart;	/* Start of first chunk */
289 GLOBAL int             *FwdLim2;	/* Next available entry */
290 GLOBAL struct FwdTable *FwdCurr;	/* Start of current chunk */
291 GLOBAL int             *FwdPtr;		/* Current position in pass 2 */
292 
293 GLOBAL int ErrorCount;
294 
295 /* Error message tables */
296 GLOBAL int ErrLim, ErrCode[ERRMAX], ErrPos[ERRMAX];
297 
298 GLOBAL int isError; /* bugfix by Kevin Kofler for the TIGCC team in v.2.71.F3a */
299 GLOBAL int noExplicitSize; /* bugfix by Kevin Kofler for the TIGCC team in v.2.71.F3b */
300 struct AddrDiff {
301     struct AddrDiff *Link;
302     long paddr, phunk, naddr, nhunk;
303 };
304 GLOBAL struct AddrDiff * AddrDiffs; /* added by Kevin Kofler in v.2.71.F3l */
305