-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpasmo.cpp
415 lines (350 loc) · 8.52 KB
/
pasmo.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
// pasmo.cpp
// Revision 15-apr-2008
#include "asm.h"
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <stdexcept>
using std::cout;
using std::cerr;
using std::endl;
namespace {
using std::string;
using std::vector;
using std::runtime_error;
const string pasmoversion (VERSION);
class Usage { };
class NeedArgument : public runtime_error {
public:
NeedArgument (const string & option) :
runtime_error ("Option " + option + " requires argument")
{ }
};
class InvalidOption : public runtime_error {
public:
InvalidOption (const string & option) :
runtime_error ("Invalid option: " + option)
{ }
};
runtime_error CreateObjectError ("Error creating object file");
runtime_error SymbolFileError ("Error creating symbols file");
runtime_error PublicFileError ("Error creating public symbols file");
std::ostream * perr= & cerr;
const string opt1 ("-1");
const string opt8 ("-8");
const string optd ("-d");
const string optv ("-v");
const string optB ("-B");
const string optE ("-E");
const string optI ("-I");
const string opt86 ("--86");
const string optalocal ("--alocal");
const string optamsdos ("--amsdos");
const string optbin ("--bin");
const string optbracket ("--bracket");
const string optcdt ("--cdt");
const string optcdtbas ("--cdtbas");
const string optcmd ("--cmd");
const string optequ ("--equ");
const string opterr ("--err");
const string opthex ("--hex");
const string optmsx ("--msx");
const string optname ("--name");
const string optnocase ("--nocase");
const string optpass3 ("--pass3");
const string optplus3dos ("--plus3dos");
const string optprl ("--prl");
const string optpublic ("--public");
const string opttap ("--tap");
const string opttapbas ("--tapbas");
const string opttzx ("--tzx");
const string opttzxbas ("--tzxbas");
const string optw8080 ("--w8080");
class Options {
public:
Options (int argc, char * * argv);
typedef void (Asm::* emitfunc_t) (std::ostream &);
emitfunc_t getemit () const { return emitfunc; }
bool redirerr () const { return redirecterr; }
bool publiconly () const { return emitpublic; }
bool getpass3 () const { return pass3; }
string getfilein () const { return filein; }
string getfileout () const { return fileout; }
string getfilesymbol () const { return filesymbol; }
string getfilepublic () const;
string getheadername () const { return headername; }
void apply (Asm & assembler) const;
private:
emitfunc_t emitfunc;
static const emitfunc_t emitdefault;
bool verbose;
bool emitpublic;
Asm::DebugType debugtype;
bool redirecterr;
bool nocase;
bool autolocal;
bool bracketonly;
bool warn8080;
bool mode86;
bool pass3;
vector <string> includedir;
vector <string> labelpredef;
string filein;
string fileout;
string filesymbol;
string filepublic;
string headername;
};
const Options::emitfunc_t Options::emitdefault (& Asm::emitobject);
Options::Options (int argc, char * * argv) :
emitfunc (emitdefault),
verbose (false),
emitpublic (false),
debugtype (Asm::NoDebug),
redirecterr (false),
nocase (false),
autolocal (false),
bracketonly (false),
warn8080 (false),
mode86 (false),
pass3 (false)
{
int argpos;
for (argpos= 1; argpos < argc; ++argpos)
{
const string arg (argv [argpos] );
if (arg == optbin)
emitfunc= & Asm::emitobject;
else if (arg == opthex)
emitfunc= & Asm::emithex;
else if (arg == optprl)
emitfunc= & Asm::emitprl;
else if (arg == optcmd)
emitfunc= & Asm::emitcmd;
else if (arg == optpass3)
pass3= true;
else if (arg == optplus3dos)
emitfunc= & Asm::emitplus3dos;
else if (arg == opttap)
emitfunc= & Asm::emittap;
else if (arg == opttzx)
emitfunc= & Asm::emittzx;
else if (arg == optcdt)
emitfunc= & Asm::emitcdt;
else if (arg == opttapbas)
emitfunc= & Asm::emittapbas;
else if (arg == opttzxbas)
emitfunc= & Asm::emittzxbas;
else if (arg == optcdtbas)
emitfunc= & Asm::emitcdtbas;
else if (arg == optamsdos)
emitfunc= & Asm::emitamsdos;
else if (arg == optmsx)
emitfunc= & Asm::emitmsx;
else if (arg == optpublic)
emitpublic= true;
else if (arg == optname)
{
++argpos;
if (argpos >= argc)
throw NeedArgument (optname);
headername= argv [argpos];
}
else if (arg == optv)
verbose= true;
else if (arg == optd)
debugtype= Asm::DebugSecondPass;
else if (arg == opt1)
debugtype= Asm::DebugAll;
else if (arg == opterr)
redirecterr= true;
else if (arg == optnocase)
nocase= true;
else if (arg == optalocal)
autolocal= true;
else if (arg == optB)
bracketonly= true;
else if (arg == optbracket)
bracketonly= true;
else if (arg == opt8 || arg == optw8080)
warn8080= true;
else if (arg == opt86)
mode86= true;
else if (arg == optI)
{
++argpos;
if (argpos >= argc)
throw NeedArgument (optI);
//a.addincludedir (argv [argpos] );
includedir.push_back (argv [argpos] );
}
else if (arg == optE || arg == optequ)
{
++argpos;
if (argpos >= argc)
throw NeedArgument (arg);
labelpredef.push_back (argv [argpos] );
}
else if (arg == "--")
{
++argpos;
break;
}
else if (arg.substr (0, 1) == "-")
throw InvalidOption (arg);
else
break;
}
// File parameters.
if (argpos >= argc)
throw Usage ();
filein= argv [argpos];
++argpos;
if (argpos >= argc)
throw Usage ();
fileout= argv [argpos];
++argpos;
if (argpos < argc)
{
filesymbol= argv [argpos];
++argpos;
if (! emitpublic && argpos < argc)
{
filepublic= argv [argpos];
++argpos;
}
if (argpos < argc)
cerr << "WARNING: Extra arguments ignored" << endl;
}
if (headername.empty () )
headername= fileout;
}
string Options::getfilepublic () const
{
if (emitpublic)
return filesymbol;
else
return filepublic;
}
void Options::apply (Asm & assembler) const
{
assembler.setdebugtype (debugtype);
if (verbose)
assembler.verbose ();
if (redirecterr)
assembler.errtostdout ();
if (nocase)
assembler.caseinsensitive ();
if (autolocal)
assembler.autolocal ();
if (bracketonly)
assembler.bracketonly ();
if (warn8080)
assembler.warn8080 ();
if (mode86)
assembler.set86 ();
if (pass3)
assembler.setpass3 ();
for (size_t i= 0; i < includedir.size (); ++i)
assembler.addincludedir (includedir [i] );
for (size_t i= 0; i < labelpredef.size (); ++i)
assembler.addpredef (labelpredef [i] );
assembler.setheadername (headername);
}
int doit (int argc, char * * argv)
{
// Process command line options.
Options option (argc, argv);
if (option.redirerr () )
perr= & cout;
// Assemble.
Asm assembler;
option.apply (assembler);
assembler.loadfile (option.getfilein () );
assembler.processfile ();
// Generate ouptut file.
std::ofstream out (option.getfileout ().c_str (),
std::ios::out | std::ios::binary);
if (! out.is_open () )
throw CreateObjectError;
(assembler.* option.getemit () ) (out);
out.close ();
// Generate symbol table and public symbol table if required.
string filesymbol= option.getfilesymbol ();
if (! option.publiconly () && ! filesymbol.empty () )
{
std::ofstream sout;
std::streambuf * cout_buf= 0;
if (filesymbol != "-")
{
sout.open (filesymbol.c_str () );
if (! sout.is_open () )
throw SymbolFileError;
cout_buf= cout.rdbuf ();
cout.rdbuf (sout.rdbuf () );
}
assembler.dumpsymbol (cout);
if (cout_buf)
{
cout.rdbuf (cout_buf);
sout.close ();
}
}
string filepublic= option.getfilepublic ();
if (! filepublic.empty () )
{
std::ofstream sout;
std::streambuf * cout_buf= 0;
if (filepublic != "-")
{
sout.open (filepublic.c_str () );
if (! sout.is_open () )
throw PublicFileError;
cout_buf= cout.rdbuf ();
cout.rdbuf (sout.rdbuf () );
}
assembler.dumppublic (cout);
if (cout_buf)
{
cout.rdbuf (cout_buf);
sout.close ();
}
}
return 0;
}
} // namespace
int main (int argc, char * * argv)
{
// Just call doit and show possible errors.
try
{
return doit (argc, argv);
}
catch (std::logic_error & e)
{
* perr << "ERROR: " << e.what () << endl <<
"This error is unexpected, please "
"send a bug report." << endl;
}
catch (std::exception & e)
{
* perr << "ERROR: " << e.what () << endl;
}
catch (Usage &)
{
cerr << "Pasmo v. " << pasmoversion <<
" (C) 2004-2008 Julian Albo\n\n"
"Usage:\n\n"
"\tpasmo [options] source object [symbol]\n\n"
"See the README file for details.\n";
}
catch (...)
{
cerr << "ERROR: Unexpected exception.\n"
"Please send a bug report.\n";
}
// Added to fix Debian bug report #394733
return 1;
}
// End of pasmo.cpp