-
Notifications
You must be signed in to change notification settings - Fork 2
/
outfile.c
215 lines (195 loc) · 5.26 KB
/
outfile.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
// 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 interface to all supported output file types
// NOTE: this has an interface similar to the processor support code.
// It allows the list of output file routines to be created at
// run time.
#include "include.h"
static OUTPUTFILE_TYPE
*topOutputFileType=NULL; // list of symbol file types (created at run time)
static SYM_TABLE
*outputFileTypeSymbols; // symbol table of names of all supported generators
struct OUTPUT_RECORD
{
OUTPUTFILE_TYPE
*type; // tells the type to output
OUTPUT_RECORD
*nextRecord; // points to the next type of output file to generate
char
outputName[1]; // variable length string which contains the file name to output to
};
static OUTPUT_RECORD
*firstOutputRecord, // linked list of output records
*lastOutputRecord;
bool DumpOutputFiles()
// dump output files for each type requested
// if there is a problem, complain and return false
{
bool
fail;
OUTPUT_RECORD
*currentRecord;
FILE
*file;
fail=false;
currentRecord=firstOutputRecord;
while(currentRecord&&!fail)
{
if((file=currentRecord->type->binary?OpenBinaryOutputFile(¤tRecord->outputName[0]):OpenTextOutputFile(¤tRecord->outputName[0])))
{
fail=!currentRecord->type->outputFileGenerateFunction(file);
currentRecord->type->binary?CloseBinaryOutputFile(file):CloseTextOutputFile(file);
}
else
{
fail=true;
}
currentRecord=currentRecord->nextRecord;
}
return(!fail);
}
bool SelectOutputFileType(char *typeName,char *outputName)
// select the given output file type for generation
// If the type does not exist, or there is some other problem, complain and
// return false
{
OUTPUTFILE_TYPE
*type;
OUTPUT_RECORD
*record;
if((type=(OUTPUTFILE_TYPE *)STFindDataForName(outputFileTypeSymbols,typeName)))
{
if((record=(OUTPUT_RECORD *)NewPtr(sizeof(OUTPUT_RECORD)+strlen(outputName)+1))) // create a record and link it to the list
{
record->type=type;
record->nextRecord=NULL;
strcpy(&record->outputName[0],outputName);
if(lastOutputRecord)
{
lastOutputRecord->nextRecord=record;
lastOutputRecord=record;
}
else
{
firstOutputRecord=lastOutputRecord=record;
}
return(true);
}
else
{
ReportComplaint(true,"Failed to allocate output file record\n");
}
}
else
{
ReportComplaint(true,"Invalid output file type: %s\n",typeName);
}
return(false);
}
void UnInitOutputFileGenerate()
// undo what InitOutputFileGenerate did
{
OUTPUT_RECORD
*currentRecord,
*nextRecord;
currentRecord=firstOutputRecord; // get rid of the list of files to generate
while(currentRecord)
{
nextRecord=currentRecord->nextRecord;
DisposePtr(currentRecord);
currentRecord=nextRecord;
}
STDisposeSymbolTable(outputFileTypeSymbols);
}
bool InitOutputFileGenerate()
// initialize output file generation selection
{
bool
fail;
OUTPUTFILE_TYPE
*type;
fail=false;
firstOutputRecord=lastOutputRecord=NULL; // no symbol files to create
if((outputFileTypeSymbols=STNewSymbolTable(10)))
{
type=topOutputFileType;
while(type&&!fail)
{
if((type->symbol=STAddEntryAtEnd(outputFileTypeSymbols,type->name,type)))
{
type=type->nextType;
}
else
{
ReportComplaint(true,"Failed to add output file type %s to list\n",type->name);
fail=true;
}
}
if(!fail)
{
return(true);
}
STDisposeSymbolTable(outputFileTypeSymbols);
}
else
{
ReportComplaint(true,"Failed to create symbol table for output file types\n");
}
return(false);
}
void DumpOutputFileTypeInformation(FILE *file)
// tell what output file types are supported by the assembler
{
OUTPUTFILE_TYPE
*type;
type=topOutputFileType;
while(type)
{
fprintf(file,"%-20s%s\n",type->name,type->description);
type=type->nextType;
}
}
// Constructors used to link output file generators to global list at run-time
OUTPUTFILE_TYPE::OUTPUTFILE_TYPE(const char *formatName,const char *formatDescription,bool isBinary,OutputFileGenerateFunction *generateFunction)
// Use this to add a given symbol file type
// to the global list
{
name=formatName;
description=formatDescription;
binary=isBinary;
outputFileGenerateFunction=generateFunction;
if((nextType=topOutputFileType)) // link to next one
{
topOutputFileType->previousType=this; // link next one to this one
}
previousType=NULL; // none previous to this
topOutputFileType=this; // point at this one
}
OUTPUTFILE_TYPE::~OUTPUTFILE_TYPE()
// When a output type goes out of scope, get rid of it here
{
if(nextType)
{
nextType->previousType=previousType; // point next's previous to our previous
}
if(previousType)
{
previousType->nextType=nextType; // point previous' next to our next
}
else
{
topOutputFileType=nextType; // if no previous, then we were the top, so set top to our next
}
}