-
Notifications
You must be signed in to change notification settings - Fork 2
/
macro.c
260 lines (232 loc) · 5.89 KB
/
macro.c
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
// Copyright (C) 1999-2012 Core Technologies.
//
// This file is part of tpasm.
//
// tpasm is free software; you can redistribute it and/or modify
// it under the terms of the tpasm LICENSE AGREEMENT.
//
// tpasm is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// tpasm LICENSE AGREEMENT for more details.
//
// You should have received a copy of the tpasm LICENSE AGREEMENT
// along with tpasm; see the file "LICENSE.TXT".
// Handle creation and manipulation of macros, and other text block related functions
#include "include.h"
static SYM_TABLE
*macroSymbols; // macro symbol list is kept here
void DestroyTextBlockLines(TEXT_BLOCK *block)
{
TEXT_LINE
*tempLine;
while(block->firstLine) // get rid of lines of text
{
tempLine=block->firstLine->next;
DisposePtr(block->firstLine);
block->firstLine=tempLine;
}
}
bool AddLineToTextBlock(TEXT_BLOCK *block,const char *line)
// Add line to block
{
unsigned int
length;
bool
fail;
TEXT_LINE
*textLine;
fail=false;
length=strlen(line);
if((textLine=(TEXT_LINE *)NewPtr(sizeof(TEXT_LINE)+length+1)))
{
textLine->next=NULL;
textLine->whereFrom.file=currentVirtualFile;
textLine->whereFrom.fileLineNumber=currentVirtualFileLine;
strcpy(&textLine->line[0],line);
if(block->lastLine)
{
block->lastLine->next=textLine; // link onto the end
block->lastLine=textLine;
}
else
{
block->firstLine=block->lastLine=textLine; // link in as first line
}
}
else
{
fail=true;
}
return(!fail);
}
bool CreateParameterList(const char *line,unsigned int *lineIndex,TEXT_BLOCK *block)
// Parse out a list of text elements
// return false if there was a hard error
{
char
element[MAX_STRING];
bool
fail;
fail=false;
if(ParseFirstListElement(line,lineIndex,element))
{
fail=!AddLineToTextBlock(block,element);
while(!fail&&ParseNextListElement(line,lineIndex,element))
{
fail=!AddLineToTextBlock(block,element);
}
}
if(fail)
{
ReportComplaint(true,"Failed to create parameter list element\n");
}
return(!fail);
}
bool CreateParameterNames(const char *line,unsigned int *lineIndex,TEXT_BLOCK *block)
// Parse out a list of name elements
// return false if there was a hard error
{
char
element[MAX_STRING];
bool
fail;
fail=false;
if(ParseFirstNameElement(line,lineIndex,element))
{
fail=!AddLineToTextBlock(block,element);
while(!fail&&ParseNextNameElement(line,lineIndex,element))
{
fail=!AddLineToTextBlock(block,element);
}
}
if(fail)
{
ReportComplaint(true,"Failed to create parameter label element\n");
}
return(!fail);
}
MACRO_RECORD *LocateMacro(const char *name)
// Attempt to locate a macro with the given name. Return its record if found, NULL if not.
{
MACRO_RECORD
*record;
if((record=(MACRO_RECORD *)STFindDataForName(macroSymbols,name)))
{
return(record);
}
return(NULL);
}
bool AttemptMacro(const char *line,unsigned int *lineIndex,LISTING_RECORD *listingRecord,bool *success)
// See if the next thing on the line looks like a macro invocation, if so, handle it.
// If this matches anything, it will set success true.
// If there's some sort of hard failure, this will return false
{
bool
result;
unsigned int
tempIndex;
char
string[MAX_STRING];
MACRO_RECORD
*record;
TEXT_BLOCK
paramValues;
result=true; // assume no hard failure
*success=false; // so far, no macro matched
tempIndex=*lineIndex;
if(ParseName(line,&tempIndex,string)) // something that looks like a macro?
{
if((record=(MACRO_RECORD *)STFindDataForNameNoCase(macroSymbols,string)))
{
*lineIndex=tempIndex; // actually push forward on the line
*success=true;
paramValues.firstLine=paramValues.lastLine=NULL;
if((result=CreateParameterList(line,lineIndex,¶mValues)))
{
if(ParseComment(line,lineIndex))
{
listingRecord->sourceType='m'; // output a small 'm' on lines which invoke a macro
OutputListFileLine(listingRecord,line); // output the line first so that the macro contents follow it
listingRecord->wantList=false;
result=ProcessTextBlock(&record->contents,&record->parameters,¶mValues,'M'); // process text of macro back into assembly stream
}
else
{
AssemblyComplaint(NULL,true,"Ill formed macro parameters\n");
}
}
DestroyTextBlockLines(¶mValues);
}
}
return(result);
}
void DestroyMacro(MACRO_RECORD *macro)
// remove macro from existence
{
DestroyTextBlockLines(¯o->parameters);
DestroyTextBlockLines(¯o->contents);
STRemoveEntry(macroSymbols,macro->symbol);
if(macro->next)
{
macro->next->previous=macro->previous;
}
if(macro->previous)
{
macro->previous->next=macro->next;
}
else
{
macrosHead=macro->next;
}
DisposePtr(macro);
}
void DestroyMacros()
// remove all macros, and all symbols from the symbol table
{
while(macrosHead)
{
DestroyMacro(macrosHead);
}
}
MACRO_RECORD *CreateMacro(char *macroName)
// Create a macro record, link it to the head of the global list, create a symbol table entry for it
{
MACRO_RECORD
*record;
if((record=(MACRO_RECORD *)NewPtr(sizeof(MACRO_RECORD))))
{
record->parameters.firstLine=NULL;
record->parameters.lastLine=NULL;
record->contents.firstLine=NULL;
record->contents.lastLine=NULL;
record->whereFrom.file=currentVirtualFile;
record->whereFrom.fileLineNumber=currentVirtualFileLine;
if((record->symbol=STAddEntryAtEnd(macroSymbols,macroName,record)))
{
record->previous=NULL;
if((record->next=macrosHead))
{
record->next->previous=record; // make reverse link
}
macrosHead=record;
return(record);
}
DisposePtr(record);
}
return(NULL);
}
void UnInitMacros()
// undo what InitMacros did
{
STDisposeSymbolTable(macroSymbols);
}
bool InitMacros()
// initialize symbol table for macros
{
if((macroSymbols=STNewSymbolTable(0)))
{
return(true);
}
return(false);
}