-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMACRO.CPP
More file actions
411 lines (361 loc) · 15.1 KB
/
Copy pathMACRO.CPP
File metadata and controls
411 lines (361 loc) · 15.1 KB
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
/***********************************************************************
MACRO.CPP
This file contains the code for assembling macros. The macro is
defined using the syntax:
macName macro
macro code
endm
Up to 37 parameters may be specified as \0 thru \9 & \A thru \Z.
Conditional assembly is supported. The syntax is:
Functions: macro - Defines the macro.
Saves file pointer to macro, defines macro name,
moves file pointer past ENDM directive.
asmMacro -
for (each line of macro) {
if macro label, define
perform parameter substitution
assemble this line of macro
}
tokenize - Tokenize a string to tokens[].
Each element of token[] is a pointer to the corresponding
token in tokens[]. is always reserved for the label
if any. A value of 0 in token[] indicates no token.
Each token is null terminated.
Each token is converted to upper case.
Author: Charles Kelly
Feb-13-2002
Monroe County Community College
* http://www.wowgwep.com/EASy68K.htm
************************************************************************/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "asm.h"
extern char line[256]; // Source line
extern FILE *inFile; // source file
extern FILE *listFile; // Listing file
extern FILE *errFile; // error message file
extern FILE *tmpFile;
extern bool continuation; // TRUE if the listing line is a continuation
extern char pass; // pass counter
extern bool pass2; // Flag set during second pass
extern int loc; // The assembler's location counter
extern int lineNum;
extern int errorCount, warningCount;
extern int labelNum; // macro label \@ number
extern bool MEXflag; // true expands macro listing
extern bool skipList; // true to skip listing line in ASSEMBLE.CPP
extern char empty[]; // used in conditional assembly
extern bool skipCond; // true skips lines in macro
extern bool printCond; // true to print condition on listing line
extern int nestLevel; // nesting level of conditional directives
extern bool skipCreateCode; // true to skip calling createCode during macro processing
int macroFP; // location of current macro in tmpFile
const int MAC_SIZE = 512; // maximun size of macro line
int macroNestLevel; // count nested macro calls
char lineIdent[MACRO_NEST_LIMIT+2]; // "mmm" used to identify macro in listing + 1 for 's' when structured code is called from macro and +1 for '\0'
bool noENDM; // set true if no ENDM in macro
//--------------------------------------------------------
// Define macro
// Save file pointer to macro, define macro name, move file pointer
// past ENDM directive.
int macro(int size, char *label, char *op, int *errorPtr)
{
symbolDef *symbol;
int error;
const int MAXT = 128; // maximum number of tokens
char *token[MAXT]; // pointers to tokens
char tokens[MAC_SIZE]; // place tokens here
if (size)
NEWERROR(*errorPtr, INV_SIZE_CODE);
error = OK;
fseek(tmpFile,0,SEEK_END); // prepare tmpFile to receive next macro
if (pass == 0)
macroFP = ftell(tmpFile); // save location of macro in tempFile
// put macro and it's file pointer in symbol table
exprVal expr;
expr.value = macroFP;
expr.isRelative = false;
symbol = define(label, expr, pass2, true, &error);
if (error == MULTIPLE_DEFS) { // ignore all errors except MULTIPLE_DEFS
NEWERROR(*errorPtr, MULTIPLE_DEFS);
return NORMAL;
}
symbol->flags |= MACRO_SYM; // set MACRO_SYM flag
if (pass2 && listFlag)
listLine(line);
// move file pointer past ENDM directive
while(fgets(line, 256, inFile)) {
if (pass == 0)
fprintf(tmpFile, "%s", line); // write macro line to tmpFile // RA warning: format not a string literal and no format arguments
lineNum++;
tokenize(line, (char *) " \t\n", token, tokens); // RA warning: ISO C++ forbids converting a string constant to ‘char*’
if(!(stricmp(token[1], "MACRO"))) { // if unexpected MACRO opcode
NEWERROR(*errorPtr, NO_ENDM); // no ENDM found
noENDM = true;
return SEVERE; // RA NULL
}
if(!(stricmp(token[1], "ENDM"))) // if ENDM opcode
return NORMAL;
if (pass2 && listFlag)
listLine(line);
}
NEWERROR(*errorPtr, NO_ENDM); // no ENDM found
noENDM = true;
return SEVERE; // RA NULL
}
//--------------------------------------------------------
// Assemble macro
// pre: macroFP contains file pointer to macro
// for (each line of macro) {
// if macro label, define
// perform parameter substitution
// assemble this line of macro
// }
int asmMacro(int size, char *label, char *arg, int *errorPtr)
{
char capLine[256], macLine[MAC_SIZE], labelNumA[16];
char *capL, *macL;
char arguments[MAX_ARGS][ARG_SIZE+1];
const int MAXT = 128; // maximum number of tokens
const int MAX_SIZE = 512; // maximun size of input line
char *token[MAXT]; // pointers to tokens
char tokens[MAX_SIZE]; // place tokens here
int error, argN, i, n;
int tempFP; // temporary File Pointer
int value;
bool backRef;
bool textArg; // true for 'text' argument
bool endmFlag; // set true by ENDM instruction
// clear arguments[] array
for (argN=0; argN < MAX_ARGS; argN++) // for all of arguments[] array
arguments[argN][0] = '\0';
// *ck 12-6-2005 added following to support size extensions on macro calls.
// Argument \0 is reserved for size and defaults to .W
switch (size) {
case BYTE_SIZE:
strcpy(arguments[0],"B");
break;
case WORD_SIZE:
strcpy(arguments[0],"W");
break;
case LONG_SIZE:
strcpy(arguments[0],"L");
break;
default:
strcpy(arguments[0],"W");
}
argN = 0;
macroNestLevel++; // count nested macro calls
if (macroNestLevel > MACRO_NEST_LIMIT) { // if nested too deep
NEWERROR(*errorPtr, MACRO_NEST); // error, probably in recursive macro call
macroNestLevel--; // count nested macro calls
return NORMAL;
}
// build macro "mmm" identifier for listing
for (i=0; i < macroNestLevel; i++)
lineIdent[i] = 'm';
lineIdent[i] = '\0';
// Define the label attached to this macro call, if any
if (*label)
define(label, LocExpr(), pass2, true, errorPtr);
// parse macro call and put arguments into array
strcap(capLine, arg);
capL = capLine;
if (*capL)
argN = 1;
while (*capL) { // loop until out of arguments
i = 0;
textArg = false;
while ( *capL && ( (*capL != ',' && !(isspace((unsigned char)*capL)) ) || textArg )) {
if (*capL == '<') { // if < *ck 12-7-2005
if (!textArg) // if not text mode
textArg = true; // set text mode flag
else
arguments[argN][i++] = *capL; // copy < to argument
} else if (*capL == '>') {
if (textArg) // if text mode
textArg = false; // turn off text mode
else
arguments[argN][i++] = *capL; // copy > to argument
} else if (!textArg && (*capL == '\'' && *(capL+1) == '\'')) // if ''
capL++; // skip ' (NULL Argument)
else if (i < ARG_SIZE && *capL != '\n')
arguments[argN][i++] = *capL; // put argument in arguments[]
capL++;
}
arguments[argN][i] = '\0'; // terminate argument
if (*capL == ',') { // if more arguments remain
capL++; // skip ','
if (*capL == '\n') { // if ',' was last char on line
// macro parameters continue on next line
if (pass2) {
if (listFlag) {
printError(listFile, *errorPtr, lineNum);
if (!skipList)
listLine(line, lineIdent);
} else {
printError(NULL, *errorPtr, lineNum);
}
}
if (fgets(line, 256, inFile) == NULL) { // get next line
NEWERROR(*errorPtr, INVALID_ARG);
macroNestLevel--; // count nested macro calls
return NORMAL;
}
strcap(capLine, line);
capL = capLine;
error = OK;
continuation = false;
if (pass2 && listFlag)
listLoc();
if (*capL != '&') { // continuation line must start with '&'
NEWERROR(*errorPtr, INVALID_ARG);
macroNestLevel--; // count nested macro calls
return NORMAL;
}
*capL++;
}
capL = skipSpace(capL); // skip spaces
if (!(*capL)) { // if syntax error
NEWERROR(*errorPtr, SYNTAX);
macroNestLevel--; // count nested macro calls
return NORMAL;
}
argN++; // next spot in arguments[]
if (argN >= MAX_ARGS) { // if too many arguments
NEWERROR(*errorPtr, TOO_MANY_ARGS);
macroNestLevel--; // count nested macro calls
return NORMAL;
}
}
else break;
}
if (pass2 && listFlag && !skipList) { // if macro call should be listed
listLoc();
listLine(line, lineIdent);
}
// position tempFile to macro definition
fseek(tmpFile, macroFP, SEEK_SET); // move to macro definition
// send each line of macro to assembler
labelNum++; // increment macro label number
//itoa(labelNum, labelNumA, 10); // RAconvert labelNum to string
snprintf(labelNumA, 16, "%d", labelNum); // RA
endmFlag = false;
while(!endmFlag && fgets(line, 256, tmpFile)) {
REMOVECR(line);
strcap(capLine, line);
tokenize(capLine, (char *)", \t\n", token, tokens); // tokenize line //Ra (char *) to suppress warning: ISO C++ forbids converting a string constant to ‘char*’
error = OK;
skipList = false;
printCond = false;
macL = macLine;
capL = capLine;
while(*capL && isspace((unsigned char)*capL)) // copy spaces
*macL++ = *capL++;
if (*capL == '*') { // if comment line
while(*capL) // copy rest of line
*macL++ = *capL++;
} else { // else not comment line
if (skipCond) // if code conditionally skipped
while(*capL)
*macL++ = *capL++; // just copy line to check for ENDC
else { // else, include code
// do macro parameter substitution and label generation
while (*capL) { // while not empty
while (*capL && !(isspace((unsigned char)*capL))) { // while not empty and not space
if (*capL == '\\') { // if macro label or parameter
capL++;
if (*capL == '@') { // if \@ macro label
capL++;
*macL++ = '_'; // add '_'
i = 0; // add labelNum to macro label
while(labelNumA[i]) {
*macL++ = labelNumA[i++];
}
} else if (isalnum(*capL)) { // if alpha numeric
n = -1;
if (isdigit(*capL)) // if parameter \0 - \9
n = *capL++ - '0'; // convert parameter to integer
else if (*capL >= 'A' && *capL <= 'Z') // if parameter \A - \Z
n = *capL++ - 'A' + 10; // convert parameter to integer
else // invalid argument
NEWERROR(error, INVALID_ARG);
if (n >= 0 && n < MAX_ARGS) { // if valid argument number
i = 0;
while(arguments[n][i]) {
*macL++ = arguments[n][i++];
}
} else
NEWERROR(error, INVALID_ARG);
} else
NEWERROR(error, SYNTAX);
// if NARG opcode
} else if( !(stricmp(token[1], "NARG")) ) {
//} else if( !(strncasecmp(capL, "NARG", 4)) && !(isalnum(*(capL+4))) ) { // RA strnicmp
sprintf(macL,"%d",argN);
if (argN > 9)
macL++;
macL++;
capL += 4;
} else {
*macL++ = *capL++; // copy macro line
}
}
while(*capL && isspace((unsigned char)*capL)) // copy spaces
*macL++ = *capL++;
}
} // end else, include code
} // end if comment, else not comment
*macL = '\0';
tempFP = ftell(tmpFile); // save current file position to support nested macros *ck 12-1-2005
continuation = false;
strcpy(line,macLine); // replace original source line with macro line
if (!MEXflag)
skipList = true;
skipCreateCode = false;
// pre process macro commands
// ----- ENDM and MEXIT -----
if( !(stricmp(token[1], "ENDM")) || // if ENDM opcode or
(!(stricmp(token[1],"MEXIT"))) && !skipCond) { // MEXIT
if (token[0] != empty) // if label present
NEWERROR(*errorPtr, LABEL_ERROR);
endmFlag = true;
skipCreateCode = true;
// ----- IFARG -----
} else if(!(stricmp(token[1], "IFARG"))) { // if IFARG opcode
if (token[0] != empty) // if label present
NEWERROR(*errorPtr, LABEL_ERROR);
if (token[2] == empty) { // if IFARG argument missing
NEWERROR(*errorPtr, INVALID_ARG);
} else {
exprVal expr;
eval(token[2], &expr, &backRef, &error);
value = expr.value;
//value--;
if (error < ERRORN && value > 0 && value < MAX_ARGS) { // if valid arg number
if (arguments[value][0] == '\0') { // if argument does not exist
skipCond = true; // skip lines in macro
nestLevel++; // nest level of skip
}
} else { // else, invalid arg number
NEWERROR(*errorPtr, INVALID_ARG);
}
}
printCond = true;
skipCreateCode = true;
}
if(!noENDM) // if no missing ENDM errors
assemble(line,&error); // this supports structured statements in macros
macroFP = tempFP; // restore macroFP
fseek(tmpFile, macroFP, SEEK_SET); // position tmpFile to previous spot *ck 12-1-2005
} // end while more lines of macro remain
skipCreateCode = false;
macroNestLevel--; // count nested macro calls
// build macro "mmm" identifier for listing
for (i=0; i < macroNestLevel; i++)
lineIdent[i] = 'm';
lineIdent[i] = '\0';
skipList = true; // don't display ENDM twice
return NORMAL;
}