-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtpasm.c
1049 lines (959 loc) · 26 KB
/
tpasm.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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 1999-2018 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".
// Cross Assembler
#include "include.h"
struct TOKEN_LIST
{
const char
*token;
unsigned int
value;
};
enum
{
T_HELP,
T_OUTPUT,
T_DIRECTORY,
T_DEFINE,
T_PROCESSOR,
T_PASSES,
T_LIST_NAME,
T_STRICT_PSEUDO,
T_WARNINGS,
T_DEBUG,
T_SHOW_PROCESSORS,
T_SHOW_OUTPUT_TYPES,
};
static const TOKEN_LIST
cmdTokens[]=
{
{"-h",T_HELP},
{"--help",T_HELP},
{"-o",T_OUTPUT},
{"-I",T_DIRECTORY},
{"-d",T_DEFINE},
{"-P",T_PROCESSOR},
{"-n",T_PASSES},
{"-l",T_LIST_NAME},
{"-s",T_STRICT_PSEUDO},
{"-w",T_WARNINGS},
{"-p",T_DEBUG},
{"-show_procs",T_SHOW_PROCESSORS},
{"-show_types",T_SHOW_OUTPUT_TYPES},
{"",0}
};
static bool MatchToken(char *string,const TOKEN_LIST *list,unsigned int *token)
// given a string, see if it matches any of the tokens in the token list, if so, return
// its token number in token, otherwise, return false
{
unsigned int
i;
bool
found,
reachedEnd;
found=reachedEnd=false;
i=0;
while(!found&&!reachedEnd)
{
if(list[i].token[0]!='\0')
{
if(strcmp(string,list[i].token)==0)
{
found=true;
*token=list[i].value;
}
i++;
}
else
{
reachedEnd=true;
}
}
return(found);
}
bool ProcessLineLocationLabel(const PARSED_LABEL *parsedLabel)
// When the label on a line is meant to be interpreted as labelling the current
// location, call this to handle it. NOTE: parsedLabel is passed in as NULL, it
// indicates there was no label on the line.
{
bool
fail;
fail=false;
if(parsedLabel)
{
if(currentSegment)
{
fail=!AssignLabel(parsedLabel,currentSegment->currentPC+currentSegment->codeGenOffset); // assign this label now, since no assembler pseudo-op was located which may have changed its meaning
}
else
{
AssemblyComplaint(NULL,true,"Label '%s' defined outside of segment\n",parsedLabel->name);
}
}
return(!fail);
}
static bool ParseLine(char *line,LISTING_RECORD *listingRecord)
// Handle parsing and assembling.
// Return true if there was no "hard" failure.
{
bool
result;
bool
hadMatch;
PARSED_LABEL
parsedLabel,
*lineLabel;
unsigned int
lineIndex;
char
string[MAX_STRING];
result=true; // assume no hard failure
lineIndex=0;
lineLabel=NULL;
hadMatch=false; // nothing has matched this line yet
if(SkipWhiteSpace(line,&lineIndex)||(lineLabel=(ParseLabelDefinition(line,&lineIndex,&parsedLabel)?&parsedLabel:NULL))||ParseComment(line,&lineIndex)) // does the line begin correctly?
{
if((result=HandleAliasMatches(line,&lineIndex,listingRecord))) // substitute opcode/operand aliases
{
if((result=AttemptGlobalPseudoOpcode(line,&lineIndex,lineLabel,listingRecord,&hadMatch)))
{
if(!hadMatch)
{
if(contextStack->active) // if not active, only a global pseudo-op can solve that, so ignore these lines
{
if((result=AttemptProcessorPseudoOpcode(line,&lineIndex,lineLabel,listingRecord,&hadMatch)))
{
if(!hadMatch)
{
if((result=ProcessLineLocationLabel(lineLabel))) // label must be for the current location, so process it
{
if((result=AttemptProcessorOpcode(line,&lineIndex,listingRecord,&hadMatch)))
{
if(!hadMatch)
{
if((result=AttemptMacro(line,&lineIndex,listingRecord,&hadMatch)))
{
if(!hadMatch)
{
if(ParseNonWhiteSpace(line,&lineIndex,string)) // something still remains?
{
AssemblyComplaint(NULL,true,"Unrecognized opcode '%s'\n",string);
}
}
}
}
}
}
}
}
}
}
}
}
}
else
{
if(contextStack->active)
{
AssemblyComplaint(NULL,true,"Garbage at start of line\n");
}
}
return(result);
}
static bool AssembleLine(char *line,char sourceType,bool wantList)
// Assemble the current source line
// If there is a problem, (hard error), complain and return false
// If there is a syntax error, or some sort of assembly problem
// Do not complain about it until the last pass (assembly errors
// are not considered failure for this routine).
{
bool
fail;
LISTING_RECORD
listingRecord;
TEXT_BLOCK
*wasCollecting;
fail=false;
listingRecord.lineNumber=currentVirtualFileLine;
listingRecord.listPC=0;
if(currentSegment)
{
listingRecord.listPC=currentSegment->currentPC;
}
listingRecord.listObjectString[0]='\0';
listingRecord.wantList=wantList;
listingRecord.sourceType=sourceType;
// ### this is a little ugly.
// We want to be able to collect the text BETWEEN the start and end pseudo-ops
// this solves the problem
wasCollecting=collectingBlock;
if(ParseLine(line,&listingRecord))
{
// generate list output
OutputListFileLine(&listingRecord,line); // dump the generated bytes to the list file
if(wasCollecting&&collectingBlock) // if nothing done to change block collection status, then collect the text line
{
if(!AddLineToTextBlock(collectingBlock,line))
{
ReportComplaint(true,"Failed to create text line during text block collection\n");
fail=true;
}
}
}
else
{
fail=true;
}
return(!fail);
}
static bool GetLine(FILE *file,char *line,unsigned int lineLength,bool *atEOF,bool *overflow)
// read a line from the given file into the array line
// if there is a read error, return false
// lineLength is considered to be the maximum number of characters
// to read into the line (including the 0 terminator)
// if the line fills before a CR is hit, overflow will be set true, and the rest of the
// line will be read, but not stored
// if the EOF is hit, atEOF is set true
{
unsigned int
i;
int
result;
unsigned char
c;
bool
hadError;
bool
stopReading;
i=0; // index into output line
stopReading=hadError=(*atEOF)=(*overflow)=false;
while(!stopReading)
{
result=fgetc(file);
if(result!=EOF) // if something was read, check it, and add to output line
{
c=(unsigned char)result;
if(c=='\n'||c=='\0') // found termination?
{
stopReading=true; // yes, output line is complete
}
else
{
if(c!='\r') // see if the character should be added to the line
{
if(i<lineLength-1) // make sure there is room to store it
{
line[i++]=c; // store it if there is room
}
else
{
(*overflow)=true; // complain of overflow, but continue to the end
}
}
}
}
else
{
stopReading=true;
(*atEOF)=true; // tell caller that EOF was encountered
}
}
if(lineLength)
{
line[i]='\0'; // terminate the line if possible
}
return(!hadError); // return error status
}
bool ProcessTextBlock(TEXT_BLOCK *block,TEXT_BLOCK *substitutionList,TEXT_BLOCK *substitutionText,char sourceType)
// process the passed block of text into the assembly stream
// NOTE: if substitutionList or substitutionText is NULL, no substitutions will occur
{
char
*inBuffer; // character buffers for line parsing
TEXT_LINE
*tempLine;
unsigned int
lastScopeValue;
bool
fail;
bool
overflow;
fail=false;
if(blockDepth<MAX_BLOCK_DEPTH)
{
blockDepth++; // step deeper
lastScopeValue=scopeValue; // remember the last value so we can put it back (allows labels to be local within text substitution blocks)
scopeCount++; // forward the scope count so this block has its own local label scope
scopeValue=scopeCount;
if((inBuffer=(char *)NewPtr((int)(MAX_STRING))))
{
tempLine=block->firstLine;
stopParsing=false;
while(!fail&&!stopParsing&&tempLine)
{
currentVirtualFile=tempLine->whereFrom.file;
currentVirtualFileLine=tempLine->whereFrom.fileLineNumber;
if(substitutionList&&substitutionText) // make sure substitutions are desired
{
if(ProcessTextSubsitutions(inBuffer,&tempLine->line[0],substitutionList,substitutionText,&overflow))
{
if(overflow)
{
AssemblyComplaint(NULL,false,"Line too long, truncation occurred\n");
}
}
else
{
fail=true;
}
}
else
{
strcpy(inBuffer,&tempLine->line[0]);
}
if(!fail)
{
fail=!AssembleLine(inBuffer,sourceType,outputListingExpansions);
tempLine=tempLine->next; // do next line
}
}
DisposePtr(inBuffer);
}
else
{
ReportComplaint(true,"Could not allocate memory for line input buffer\nOS Reports: %s\n",strerror(errno));
fail=true;
}
scopeValue=lastScopeValue; // back out to the previous scope
stopParsing=false;
blockDepth--; // step back
}
else
{
AssemblyComplaint(NULL,true,"Attempt to nest text substitutions too deeply (arbitrary limit is %d)\n",MAX_BLOCK_DEPTH);
}
return(!fail);
}
bool ProcessSourceFile(const char *fileName,bool huntForIt)
// process the passed source file, if there are any problems, return false
// if intermediatePass is true, do not flag expressions which contain unresolved labels as errors,
// and do not generate output.
// if intermediatePass is false, then generate output, complaining about anything
// that could not be resolved.
// numUnresolvedLabels will be incremented for each unresolved label encountered.
// numModifiedLabels will be incremented each time a label's value is changed.
{
char
*inBuffer; // character buffers for line parsing
FILE
*sourceFile;
unsigned int
oldLineNum; // line number at entry
SYM_TABLE_NODE
*newSourceFile,
*oldSourceFile;
bool
fail;
bool
overflow;
fail=false;
if(includeDepth<MAX_INCLUDE_DEPTH)
{
includeDepth++; // step deeper
oldSourceFile=currentFile;
oldLineNum=currentFileLine; // hold these until we are through
if((sourceFile=OpenSourceFile(&fileName[0],huntForIt,&newSourceFile)))
{
if((inBuffer=(char *)NewPtr((int)(MAX_STRING))))
{
currentFile=newSourceFile;
currentFileLine=0;
stopParsing=false;
while(!fail&&!stopParsing)
{
if(GetLine(sourceFile,inBuffer,MAX_STRING,&stopParsing,&overflow))
{
currentFileLine++; // increment the line because we just read one
currentVirtualFile=currentFile;
currentVirtualFileLine=currentFileLine;
if(overflow)
{
AssemblyComplaint(NULL,false,"Line too long, truncation occurred\n");
}
fail=!AssembleLine(inBuffer,' ',true);
}
else
{
ReportComplaint(true,"Failed to read source line\n");
fail=true;
}
}
DisposePtr(inBuffer);
}
else
{
ReportComplaint(true,"Could not allocate memory for line input buffer\nOS Reports: %s\n",strerror(errno));
fail=true;
}
CloseSourceFile(sourceFile);
}
currentFile=oldSourceFile;
currentFileLine=oldLineNum;
stopParsing=false;
includeDepth--; // step back
}
else
{
ReportComplaint(true,"Attempt to nest include too deeply (arbitrary limit is %d)\n",MAX_INCLUDE_DEPTH);
fail=true;
}
return(!fail);
}
static bool ProcessAssembly()
// Assemble the source file, and all includes it may have.
// if intermediatePass is true, do not flag expressions which contain unresolved labels as errors,
// and do not generate output.
// if intermediatePass is false, then generate output, complaining about anything
// that could not be resolved.
// numUnresolvedLabels will be incremented for each unresolved label encountered.
// numModifiedLabels will be incremented each time a label's value is changed.
{
bool
fail;
bool
found;
ReportDiagnostic("pass %d\n",passCount+1);
currentFile=NULL; // pointer to the current file being assembled
currentFileLine=0;
currentVirtualFile=NULL; // clear pointer to virtual file
currentVirtualFileLine=0;
collectingBlock=NULL; // not collecting a text block yet
aliasesHead=NULL; // no operand aliases exist yet
macrosHead=NULL; // no macros exist yet
currentSegment=NULL; // no current segment yet
segmentsHead=segmentsTail=NULL; // no segments exist yet
includeDepth=0; // no includes yet
if(SelectProcessor(defaultProcessorName,&found))
{
if(found) // make sure the processor was located
{
if((currentSegment=CreateSegment("code",true))) // set up a default segment to assemble into
{
numUnresolvedLabels=numModifiedLabels=0; // reset these
scope[0]='\0'; // reset scope
scopeCount=0;
scopeValue=0;
blockDepth=0; // no macro invocations yet
contextStack=NULL; // initialize context stack
outputListing=true; // output listing information by default
outputListingExpansions=true; // output expansion of macros and repeats by default
if(PushContextRecord(0)) // create the root context
{
contextStack->contextType=CT_ROOT; // make a default context
contextStack->active=true; // declare it active
fail=!ProcessSourceFile(sourceFileName,false);// go handle this file (and any includes it may have)
FlushContextRecords(); // unwind any contexts that happen to be left around
DestroyAliases(); // get rid of alias definitions
DestroyMacros(); // dispose of macro definitions
currentFile=NULL;
currentVirtualFile=NULL;
}
else
{
ReportComplaint(true,"Failed to create root context\n");
fail=true;
}
if(!intermediatePass)
{
OutputListFileSegments();
OutputListFileLabels();
if(errorCount==0)
{
DumpOutputFiles(); // dump out all requested segment or symbol files
}
}
DestroySegments(); // remove segments created by assembly
}
else
{
ReportComplaint(true,"Failed to create default code segment\n");
fail=true;
}
SelectProcessor("",&found); // deselect processor at end of pass
}
else
{
ReportComplaint(true,"Default processor '%s' unrecognized\n",defaultProcessorName);
fail=true;
}
}
else
{
fail=true; // some hard failure in select (it was reported there)
}
return(!fail);
}
static bool HandleAssembly()
// Assemble the source file(s), create output and list files as needed
// If there is a problem, report it, and return false
{
bool
fail;
unsigned int
lastNumUnresolvedLabels;
time_t
timeIn,timeOut;
unsigned int
totalTime;
bool
keepGoing;
fail=false;
listFile=NULL;
if(!listFileName||(listFile=OpenTextOutputFile(listFileName)))
{
time(&timeIn); // get time at entry
OutputListFileHeader(timeIn);
passCount=0;
intermediatePass=true;
fail=!ProcessAssembly(); // make first pass on source
passCount++;
lastNumUnresolvedLabels=numUnresolvedLabels; // remember how many were unresolved this time
keepGoing=(numUnresolvedLabels!=0); // if on first pass, nothing was unresolved, then we're done
while(!fail&&keepGoing) // loop until we fail, or stop making progress with label resolution
{
if(ProcessAssembly()) // process the passed source file (failure here means HARD failure, not assembly errors)
{
passCount++;
ReportDiagnostic("Unresolved labels: %d, Modified labels: %d\n",numUnresolvedLabels,numModifiedLabels);
keepGoing=(((numUnresolvedLabels>0)&&(numUnresolvedLabels<lastNumUnresolvedLabels))||numModifiedLabels); // if there are still unresolved labels, but there's progess, OR something got modified, keep going
lastNumUnresolvedLabels=numUnresolvedLabels; // remember how many were unresolved this time
if(passCount>=maxPasses&&keepGoing) // hmmm, source is not resolving
{
ReportComplaint(true,"Assembly aborting due to too many passes (%d)\n",passCount);
keepGoing=false;
}
}
else
{
fail=true;
}
}
if(!fail) // make final pass, creating output/listing files, complaining about any unresolved labels that still remain
{
intermediatePass=false; // this is the final pass
fail=!ProcessAssembly(); // make final pass, generating output
time(&timeOut); // get time now that assembly is finished
totalTime=(int)difftime(timeOut,timeIn);
if(!fail)
{
OutputListFileStats(totalTime);
}
}
if(listFileName) // if list file was desired, then close it
{
CloseTextOutputFile(listFile);
}
}
else
{
fail=true;
}
return(!fail);
}
static void InitGlobals()
// initialize program globals
{
numAllocatedPointers=0;
infoOnly=false; // assume we are actually assembling
strictPseudo=false;
displayWarnings=true;
displayDiagnostics=false;
errorCount=0;
warningCount=0;
currentFile=NULL; // pointer to the current file being assembled
currentFileLine=0;
currentVirtualFile=NULL; // clear pointer to virtual file
currentVirtualFileLine=0;
maxPasses=DEFAULT_MAX_PASSES; // set default maximum number of passes
sourceFileName=NULL;
listFileName=NULL;
defaultProcessorName=""; // by default, select no processor
}
static void Usage()
// print some messages to stderr that tell how to use this program
{
fprintf(stderr,"\n");
fprintf(stderr,"%s version %s\n",programName,VERSION);
fprintf(stderr,"Copyright (C) 1999-2018 Core Technologies.\n");
fprintf(stderr,"\n");
fprintf(stderr,"Cross Assembler\n");
fprintf(stderr,"\n");
fprintf(stderr,"Usage: %s [opts] sourceFile [opts]\n",programName);
fprintf(stderr,"Assembly Options:\n");
fprintf(stderr,"\n");
fprintf(stderr," -h Output this help message\n");
fprintf(stderr," -o type fileName Output 'type' data to fileName\n");
fprintf(stderr," Multiple -o options are allowed, all will be processed\n");
fprintf(stderr," No output is generated by default\n");
fprintf(stderr," -I dir Append dir to the list of directories searched by include\n");
fprintf(stderr," -d label value Define a label to the given value\n");
fprintf(stderr," -P processor Choose initial processor to assemble for\n");
fprintf(stderr," -n passes Set maximum number of passes (default = %d)\n",DEFAULT_MAX_PASSES);
fprintf(stderr," -l listName Create listing to listName\n");
fprintf(stderr," -s Strict pseudo-ops -- limit global pseudo-ops to those that start with a dot\n");
fprintf(stderr," -w Do not report warnings\n");
fprintf(stderr," -p Print diagnostic messages to stderr\n");
fprintf(stderr,"\n");
fprintf(stderr,"\n");
fprintf(stderr,"Information Options:\n");
fprintf(stderr,"\n");
fprintf(stderr," -show_procs Dump the supported processor list\n");
fprintf(stderr," -show_types Dump the output file types list\n");
fprintf(stderr,"\n");
}
static void NotEnoughArgs(char *commandName)
// complain of inadequate number of arguments for a command
{
ReportComplaint(true,"Not enough arguments for '%s'\n",commandName);
}
static bool DoOutput(unsigned int *currentArg,unsigned int argc,char *argv[])
// register a type and name of output file
{
if((*currentArg)+3<=argc)
{
(*currentArg)++;
if(SelectOutputFileType(argv[*currentArg],argv[(*currentArg)+1]))
{
(*currentArg)+=2;
return(true);
}
}
else
{
NotEnoughArgs(argv[*currentArg]);
}
return(false);
}
static bool DoDirectory(unsigned int *currentArg,unsigned int argc,char *argv[])
// add directory to be searched for include files
{
if((*currentArg)+2<=argc)
{
(*currentArg)++;
if(AddIncludePath(argv[(*currentArg)++])) // remember this include path
{
return(true);
}
else
{
ReportComplaint(true,"Failed to add include path\n");
}
}
else
{
NotEnoughArgs(argv[*currentArg]);
}
return(false);
}
static bool DoDefine(unsigned int *currentArg,unsigned int argc,char *argv[])
// define a label
{
int
value;
if((*currentArg)+3<=argc)
{
(*currentArg)++;
value=strtol(argv[(*currentArg)+1],NULL,0);
if(CreateLabel(argv[*currentArg],value,LF_EXT_CONST,0,true))
{
(*currentArg)+=2;
return(true);
}
}
else
{
NotEnoughArgs(argv[*currentArg]);
}
return(false);
}
static bool DoProcessor(unsigned int *currentArg,unsigned int argc,char *argv[])
// set initial processor
{
if((*currentArg)+2<=argc)
{
(*currentArg)++;
defaultProcessorName=argv[(*currentArg)++];
return(true);
}
else
{
NotEnoughArgs(argv[*currentArg]);
}
return(false);
}
static bool DoPasses(unsigned int *currentArg,unsigned int argc,char *argv[])
// set initial processor
{
if((*currentArg)+2<=argc)
{
(*currentArg)++;
maxPasses=strtol(argv[(*currentArg)++],NULL,0);
return(true);
}
else
{
NotEnoughArgs(argv[*currentArg]);
}
return(false);
}
static bool DoListName(unsigned int *currentArg,unsigned int argc,char *argv[])
// set the name of the listing output file
{
if((*currentArg)+2<=argc)
{
(*currentArg)++;
listFileName=argv[(*currentArg)++];
return(true);
}
else
{
NotEnoughArgs(argv[*currentArg]);
}
return(false);
}
static bool DoStrictPseudo(unsigned int *currentArg,unsigned int argc,char *argv[])
// Limit assembler pseudo-ops to those that start with a '.'
// This keeps the non-dotted versions from colliding with opcodes for
// the selected processor.
{
(*currentArg)++;
strictPseudo=true;
return(true);
}
static bool DoWarnings(unsigned int *currentArg,unsigned int argc,char *argv[])
// set no warning display
{
(*currentArg)++;
displayWarnings=false;
return(true);
}
static bool DoDebug(unsigned int *currentArg,unsigned int argc,char *argv[])
// set debug display
{
(*currentArg)++;
displayDiagnostics=true;
return(true);
}
static bool DoShowProcessors(unsigned int *currentArg,unsigned int argc,char *argv[])
// dump the list of supported processors
{
(*currentArg)++;
fprintf(stderr,"\n");
fprintf(stderr,"Supported processors:\n");
fprintf(stderr,"\n");
DumpProcessorInformation(stderr);
fprintf(stderr,"\n");
infoOnly=true; // tell parser that user just wanted info -- no assembly
return(true);
}
static bool DoShowOutputTypes(unsigned int *currentArg,unsigned int argc,char *argv[])
// dump the list of supported output file types
{
(*currentArg)++;
fprintf(stderr,"\n");
fprintf(stderr,"Supported output types:\n");
fprintf(stderr,"\n");
DumpOutputFileTypeInformation(stderr);
fprintf(stderr,"\n");
infoOnly=true; // tell parser that user just wanted info -- no assembly
return(true);
}
static bool ParseCommandLine(unsigned int argc,char *argv[])
// Parse and interpret the command line parameters
// If there is a problem, complain and return false
{
unsigned int
currentArg;
unsigned int
token;
bool
fail;
fail=false;
currentArg=1; // skip over the program name
while(currentArg<argc&&!fail)
{
token=0; // keep compiler quiet
if(MatchToken(argv[currentArg],&cmdTokens[0],&token))
{
switch(token)
{
case T_HELP:
fail=true; // fail so that usage information will be printed
break;
case T_OUTPUT:
fail=!DoOutput(¤tArg,argc,argv);
break;
case T_DIRECTORY:
fail=!DoDirectory(¤tArg,argc,argv);
break;
case T_DEFINE:
fail=!DoDefine(¤tArg,argc,argv);
break;
case T_PROCESSOR:
fail=!DoProcessor(¤tArg,argc,argv);
break;
case T_PASSES:
fail=!DoPasses(¤tArg,argc,argv);
break;
case T_LIST_NAME:
fail=!DoListName(¤tArg,argc,argv);
break;
case T_STRICT_PSEUDO:
fail=!DoStrictPseudo(¤tArg,argc,argv);
break;
case T_WARNINGS:
fail=!DoWarnings(¤tArg,argc,argv);
break;
case T_DEBUG:
fail=!DoDebug(¤tArg,argc,argv);
break;
case T_SHOW_PROCESSORS:
DoShowProcessors(¤tArg,argc,argv);
break;
case T_SHOW_OUTPUT_TYPES:
DoShowOutputTypes(¤tArg,argc,argv);
break;
default:
currentArg++; // this token is processed
break;
}
}
else
{
if(argv[currentArg][0]!='-')
{
if(!sourceFileName)
{
sourceFileName=argv[currentArg++]; // retain this as the source file name
}
else
{
ReportComplaint(true,"Source file name already given, '%s' is invalid here\n",argv[currentArg]);
fail=true;
}
}
else
{
ReportComplaint(true,"Unrecognized command line option '%s'\n",argv[currentArg]);
fail=true;
}
}
}
if(!fail&&!sourceFileName&&!infoOnly)
{
ReportComplaint(true,"No source file name given\n");
fail=true;
}
return(!fail);
}
static void UnInitAssembler()
// Call all the uninitialization routines
{
UnInitProcessors();
UnInitAliases();
UnInitMacros();
UnInitGlobalPseudoOpcodes();
UnInitOutputFileGenerate();
UnInitLabels();
UnInitSegments();
UnInitFiles();
}
static bool InitAssembler()
// Call all the initialization routines
{
InitGlobals(); // start the ball rolling
if(InitFiles())
{
if(InitSegments()) // initialize code segment handling
{
if(InitLabels()) // initialize label handling
{
if(InitOutputFileGenerate())
{
if(InitGlobalPseudoOpcodes()) // create symbols for global pseudo ops
{
if(InitMacros()) // initialize macro handling
{
if(InitAliases())
{
if(InitProcessors()) // set up all the processors
{
return(true);
}
UnInitAliases();
}
UnInitMacros();
}
UnInitGlobalPseudoOpcodes();
}
UnInitOutputFileGenerate();
}
UnInitLabels();
}