]> git.sur5r.net Git - cc65/blob - src/ca65/main.c
Merge branch 'master' into lightpen
[cc65] / src / ca65 / main.c
1 /*                                                                           */
2 /*                                  main.c                                   */
3 /*                                                                           */
4 /*                 Main program for the ca65 macroassembler                  */
5 /*                                                                           */
6 /*                                                                           */
7 /*                                                                           */
8 /* (C) 1998-2013, Ullrich von Bassewitz                                      */
9 /*                Roemerstrasse 52                                           */
10 /*                D-70794 Filderstadt                                        */
11 /* EMail:         uz@cc65.org                                                */
12 /*                                                                           */
13 /*                                                                           */
14 /* This software is provided 'as-is', without any expressed or implied       */
15 /* warranty.  In no event will the authors be held liable for any damages    */
16 /* arising from the use of this software.                                    */
17 /*                                                                           */
18 /* Permission is granted to anyone to use this software for any purpose,     */
19 /* including commercial applications, and to alter it and redistribute it    */
20 /* freely, subject to the following restrictions:                            */
21 /*                                                                           */
22 /* 1. The origin of this software must not be misrepresented; you must not   */
23 /*    claim that you wrote the original software. If you use this software   */
24 /*    in a product, an acknowledgment in the product documentation would be  */
25 /*    appreciated but is not required.                                       */
26 /* 2. Altered source versions must be plainly marked as such, and must not   */
27 /*    be misrepresented as being the original software.                      */
28 /* 3. This notice may not be removed or altered from any source              */
29 /*    distribution.                                                          */
30 /*                                                                           */
31 /*****************************************************************************/
32
33
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <time.h>
39
40 /* common */
41 #include "addrsize.h"
42 #include "chartype.h"
43 #include "cmdline.h"
44 #include "debugflag.h"
45 #include "mmodel.h"
46 #include "print.h"
47 #include "scopedefs.h"
48 #include "strbuf.h"
49 #include "target.h"
50 #include "tgttrans.h"
51 #include "version.h"
52
53 /* ca65 */
54 #include "abend.h"
55 #include "asserts.h"
56 #include "dbginfo.h"
57 #include "error.h"
58 #include "expr.h"
59 #include "feature.h"
60 #include "filetab.h"
61 #include "global.h"
62 #include "incpath.h"
63 #include "instr.h"
64 #include "istack.h"
65 #include "lineinfo.h"
66 #include "listing.h"
67 #include "macro.h"
68 #include "nexttok.h"
69 #include "objfile.h"
70 #include "options.h"
71 #include "pseudo.h"
72 #include "scanner.h"
73 #include "segment.h"
74 #include "sizeof.h"
75 #include "span.h"
76 #include "spool.h"
77 #include "symbol.h"
78 #include "symtab.h"
79 #include "ulabel.h"
80
81
82
83 /*****************************************************************************/
84 /*                                   Code                                    */
85 /*****************************************************************************/
86
87
88
89 static void Usage (void)
90 /* Print usage information and exit */
91 {
92     printf ("Usage: %s [options] file\n"
93             "Short options:\n"
94             "  -D name[=value]\t\tDefine a symbol\n"
95             "  -I dir\t\t\tSet an include directory search path\n"
96             "  -U\t\t\t\tMark unresolved symbols as import\n"
97             "  -V\t\t\t\tPrint the assembler version\n"
98             "  -W n\t\t\t\tSet warning level n\n"
99             "  -d\t\t\t\tDebug mode\n"
100             "  -g\t\t\t\tAdd debug info to object file\n"
101             "  -h\t\t\t\tHelp (this text)\n"
102             "  -i\t\t\t\tIgnore case of symbols\n"
103             "  -l name\t\t\tCreate a listing file if assembly was ok\n"
104             "  -mm model\t\t\tSet the memory model\n"
105             "  -o name\t\t\tName the output file\n"
106             "  -s\t\t\t\tEnable smart mode\n"
107             "  -t sys\t\t\tSet the target system\n"
108             "  -v\t\t\t\tIncrease verbosity\n"
109             "\n"
110             "Long options:\n"
111             "  --auto-import\t\t\tMark unresolved symbols as import\n"
112             "  --bin-include-dir dir\t\tSet a search path for binary includes\n"
113             "  --cpu type\t\t\tSet cpu type\n"
114             "  --create-dep name\t\tCreate a make dependency file\n"
115             "  --create-full-dep name\tCreate a full make dependency file\n"
116             "  --debug\t\t\tDebug mode\n"
117             "  --debug-info\t\t\tAdd debug info to object file\n"
118             "  --feature name\t\tSet an emulation feature\n"
119             "  --help\t\t\tHelp (this text)\n"
120             "  --ignore-case\t\t\tIgnore case of symbols\n"
121             "  --include-dir dir\t\tSet an include directory search path\n"
122             "  --large-alignment\t\tDon't warn about large alignments\n"
123             "  --listing name\t\tCreate a listing file if assembly was ok\n"
124             "  --list-bytes n\t\tMaximum number of bytes per listing line\n"
125             "  --macpack-dir dir\t\tSet a macro package directory\n"
126             "  --memory-model model\t\tSet the memory model\n"
127             "  --pagelength n\t\tSet the page length for the listing\n"
128             "  --relax-checks\t\tRelax some checks (see docs)\n"
129             "  --smart\t\t\tEnable smart mode\n"
130             "  --target sys\t\t\tSet the target system\n"
131             "  --verbose\t\t\tIncrease verbosity\n"
132             "  --version\t\t\tPrint the assembler version\n",
133             ProgName);
134 }
135
136
137
138 static void SetOptions (void)
139 /* Set the option for the translator */
140 {
141     StrBuf Buf = STATIC_STRBUF_INITIALIZER;
142
143     /* Set the translator */
144     SB_Printf (&Buf, "ca65 V%s", GetVersionAsString ());
145     OptTranslator (&Buf);
146
147     /* Set date and time */
148     OptDateTime ((unsigned long) time(0));
149
150     /* Release memory for the string */
151     SB_Done (&Buf);
152 }
153
154
155
156 static void NewSymbol (const char* SymName, long Val)
157 /* Define a symbol with a fixed numeric value in the current scope */
158 {
159     ExprNode* Expr;
160     SymEntry* Sym;
161
162     /* Convert the name to a string buffer */
163     StrBuf SymBuf = STATIC_STRBUF_INITIALIZER;
164     SB_CopyStr (&SymBuf, SymName);
165
166     /* Search for the symbol, allocate a new one if it doesn't exist */
167     Sym = SymFind (CurrentScope, &SymBuf, SYM_ALLOC_NEW);
168
169     /* Check if have already a symbol with this name */
170     if (SymIsDef (Sym)) {
171         AbEnd ("`%s' is already defined", SymName);
172     }
173
174     /* Generate an expression for the symbol */
175     Expr = GenLiteralExpr (Val);
176
177     /* Mark the symbol as defined */
178     SymDef (Sym, Expr, ADDR_SIZE_DEFAULT, SF_NONE);
179
180     /* Free string buffer memory */
181     SB_Done (&SymBuf);
182 }
183
184
185
186 static void CBMSystem (const char* Sys)
187 /* Define a CBM system */
188 {
189     NewSymbol ("__CBM__", 1);
190     NewSymbol (Sys, 1);
191 }
192
193
194
195 static void SetSys (const char* Sys)
196 /* Define a target system */
197 {
198     switch (Target = FindTarget (Sys)) {
199
200         case TGT_NONE:
201             break;
202
203         case TGT_MODULE:
204             AbEnd ("Cannot use `module' as a target for the assembler");
205             break;
206
207         case TGT_ATARI:
208             NewSymbol ("__ATARI__", 1);
209             break;
210
211         case TGT_ATARIXL:
212             NewSymbol ("__ATARI__", 1);
213             NewSymbol ("__ATARIXL__", 1);
214             break;
215
216         case TGT_C16:
217             CBMSystem ("__C16__");
218             break;
219
220         case TGT_C64:
221             CBMSystem ("__C64__");
222             break;
223
224         case TGT_VIC20:
225             CBMSystem ("__VIC20__");
226             break;
227
228         case TGT_C128:
229             CBMSystem ("__C128__");
230             break;
231
232         case TGT_PLUS4:
233             CBMSystem ("__C16__");
234             NewSymbol ("__PLUS4__", 1);
235             break;
236
237         case TGT_CBM510:
238             CBMSystem ("__CBM510__");
239             break;
240
241         case TGT_CBM610:
242             CBMSystem ("__CBM610__");
243             break;
244
245         case TGT_PET:
246             CBMSystem ("__PET__");
247             break;
248
249         case TGT_BBC:
250             NewSymbol ("__BBC__", 1);
251             break;
252
253         case TGT_APPLE2:
254             NewSymbol ("__APPLE2__", 1);
255             break;
256
257         case TGT_APPLE2ENH:
258             NewSymbol ("__APPLE2__", 1);
259             NewSymbol ("__APPLE2ENH__", 1);
260             break;
261
262         case TGT_GEOS_CBM:
263             /* Do not handle as a CBM system */
264             NewSymbol ("__GEOS__", 1);
265             NewSymbol ("__GEOS_CBM__", 1);
266             break;
267
268         case TGT_GEOS_APPLE:
269             NewSymbol ("__GEOS__", 1);
270             NewSymbol ("__GEOS_APPLE__", 1);
271             break;
272
273         case TGT_LUNIX:
274             NewSymbol ("__LUNIX__", 1);
275             break;
276
277         case TGT_ATMOS:
278             NewSymbol ("__ATMOS__", 1);
279             break;
280
281         case TGT_NES:
282             NewSymbol ("__NES__", 1);
283             break;
284
285         case TGT_SUPERVISION:
286             NewSymbol ("__SUPERVISION__", 1);
287             break;
288
289         case TGT_LYNX:
290             NewSymbol ("__LYNX__", 1);
291             break;
292
293         case TGT_SIM6502:
294             NewSymbol ("__SIM6502__", 1);
295             break;
296
297         case TGT_SIM65C02:
298             NewSymbol ("__SIM65C02__", 1);
299             break;
300
301         default:
302             AbEnd ("Invalid target name: `%s'", Sys);
303
304     }
305
306     /* Initialize the translation tables for the target system */
307     TgtTranslateInit ();
308 }
309
310
311
312 static void FileNameOption (const char* Opt, const char* Arg, StrBuf* Name)
313 /* Handle an option that remembers a file name for later */
314 {
315     /* Cannot have the option twice */
316     if (SB_NotEmpty (Name)) {
317         AbEnd ("Cannot use option `%s' twice", Opt);
318     }
319     /* Remember the file name for later */
320     SB_CopyStr (Name, Arg);
321     SB_Terminate (Name);
322 }
323
324
325
326 static void DefineSymbol (const char* Def)
327 /* Define a symbol from the command line */
328 {
329     const char* P;
330     long Val;
331     StrBuf SymName = AUTO_STRBUF_INITIALIZER;
332
333
334     /* The symbol must start with a character or underline */
335     if (!IsIdStart (Def [0])) {
336         InvDef (Def);
337     }
338     P = Def;
339
340     /* Copy the symbol, checking the rest */
341     while (IsIdChar (*P)) {
342         SB_AppendChar (&SymName, *P++);
343     }
344     SB_Terminate (&SymName);
345
346     /* Do we have a value given? */
347     if (*P != '=') {
348         if (*P != '\0') {
349             InvDef (Def);
350         }
351         Val = 0;
352     } else {
353         /* We have a value */
354         ++P;
355         if (*P == '$') {
356             ++P;
357             if (sscanf (P, "%lx", &Val) != 1) {
358                 InvDef (Def);
359             }
360         } else {
361             if (sscanf (P, "%li", &Val) != 1) {
362                 InvDef (Def);
363             }
364         }
365     }
366
367     /* Define the new symbol */
368     NewSymbol (SB_GetConstBuf (&SymName), Val);
369
370     /* Release string memory */
371     SB_Done (&SymName);
372 }
373
374
375
376 static void OptAutoImport (const char* Opt attribute ((unused)),
377                            const char* Arg attribute ((unused)))
378 /* Mark unresolved symbols as imported */
379 {
380     AutoImport = 1;
381 }
382
383
384
385 static void OptBinIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
386 /* Add an include search path for binaries */
387 {
388     AddSearchPath (BinSearchPath, Arg);
389 }
390
391
392
393 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
394 /* Handle the --cpu option */
395 {
396     cpu_t CPU = FindCPU (Arg);
397     if (CPU == CPU_UNKNOWN) {
398         AbEnd ("Invalid CPU: `%s'", Arg);
399     } else {
400         SetCPU (CPU);
401     }
402 }
403
404
405
406 static void OptCreateDep (const char* Opt, const char* Arg)
407 /* Handle the --create-dep option */
408 {
409     FileNameOption (Opt, Arg, &DepName);
410 }
411
412
413
414 static void OptCreateFullDep (const char* Opt attribute ((unused)),
415                               const char* Arg)
416 /* Handle the --create-full-dep option */
417 {
418     FileNameOption (Opt, Arg, &FullDepName);
419 }
420
421
422
423 static void OptDebug (const char* Opt attribute ((unused)),
424                       const char* Arg attribute ((unused)))
425 /* Compiler debug mode */
426 {
427     ++Debug;
428 }
429
430
431
432 static void OptDebugInfo (const char* Opt attribute ((unused)),
433                           const char* Arg attribute ((unused)))
434 /* Add debug info to the object file */
435 {
436     DbgSyms = 1;
437 }
438
439
440
441 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
442 /* Set an emulation feature */
443 {
444     /* Make a string buffer from Arg */
445     StrBuf Feature;
446
447     /* Set the feature, check for errors */
448     if (SetFeature (SB_InitFromString (&Feature, Arg)) == FEAT_UNKNOWN) {
449         AbEnd ("Illegal emulation feature: `%s'", Arg);
450     }
451 }
452
453
454
455 static void OptHelp (const char* Opt attribute ((unused)),
456                      const char* Arg attribute ((unused)))
457 /* Print usage information and exit */
458 {
459     Usage ();
460     exit (EXIT_SUCCESS);
461 }
462
463
464
465 static void OptIgnoreCase (const char* Opt attribute ((unused)),
466                            const char* Arg attribute ((unused)))
467 /* Ignore case on symbols */
468 {
469     IgnoreCase = 1;
470 }
471
472
473
474 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
475 /* Add an include search path */
476 {
477     AddSearchPath (IncSearchPath, Arg);
478 }
479
480
481
482 static void OptLargeAlignment (const char* Opt attribute ((unused)),
483                                const char* Arg attribute ((unused)))
484 /* Don't warn about large alignments */
485 {
486     LargeAlignment = 1;
487 }
488
489
490
491 static void OptListBytes (const char* Opt, const char* Arg)
492 /* Set the maximum number of bytes per listing line */
493 {
494     unsigned Num;
495     char     Check;
496
497     /* Convert the argument to a number */
498     if (sscanf (Arg, "%u%c", &Num, &Check) != 1) {
499         InvArg (Opt, Arg);
500     }
501
502     /* Check the bounds */
503     if (Num != 0 && (Num < MIN_LIST_BYTES || Num > MAX_LIST_BYTES)) {
504         AbEnd ("Argument for option `%s' is out of range", Opt);
505     }
506
507     /* Use the value */
508     SetListBytes (Num);
509 }
510
511
512
513 static void OptListing (const char* Opt, const char* Arg)
514 /* Create a listing file */
515 {
516     /* Since the meaning of -l and --listing has changed, print an error if
517      * the filename is empty or begins with the option char.
518      */
519     if (Arg == 0 || *Arg == '\0' || *Arg == '-') {
520         Fatal ("The meaning of `%s' has changed. It does now "
521                "expect a file name as argument.", Opt);
522     }
523
524     /* Get the file name */
525     FileNameOption (Opt, Arg, &ListingName);
526 }
527
528
529
530 static void OptMemoryModel (const char* Opt, const char* Arg)
531 /* Set the memory model */
532 {
533     mmodel_t M;
534
535     /* Check the current memory model */
536     if (MemoryModel != MMODEL_UNKNOWN) {
537         AbEnd ("Cannot use option `%s' twice", Opt);
538     }
539
540     /* Translate the memory model name and check it */
541     M = FindMemoryModel (Arg);
542     if (M == MMODEL_UNKNOWN) {
543         AbEnd ("Unknown memory model: %s", Arg);
544     } else if (M == MMODEL_HUGE) {
545         AbEnd ("Unsupported memory model: %s", Arg);
546     }
547
548     /* Set the memory model */
549     SetMemoryModel (M);
550 }
551
552
553
554 static void OptPageLength (const char* Opt attribute ((unused)), const char* Arg)
555 /* Handle the --pagelength option */
556 {
557     int Len = atoi (Arg);
558     if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
559         AbEnd ("Invalid page length: %d", Len);
560     }
561     PageLength = Len;
562 }
563
564
565
566 static void OptRelaxChecks (const char* Opt attribute ((unused)),
567                             const char* Arg attribute ((unused)))
568 /* Handle the --relax-checks options */
569 {
570     RelaxChecks = 1;
571 }
572
573
574
575 static void OptSmart (const char* Opt attribute ((unused)),
576                       const char* Arg attribute ((unused)))
577 /* Handle the -s/--smart options */
578 {
579     SmartMode = 1;
580 }
581
582
583
584 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
585 /* Set the target system */
586 {
587     SetSys (Arg);
588 }
589
590
591
592 static void OptVerbose (const char* Opt attribute ((unused)),
593                         const char* Arg attribute ((unused)))
594 /* Increase verbosity */
595 {
596     ++Verbosity;
597 }
598
599
600
601 static void OptVersion (const char* Opt attribute ((unused)),
602                         const char* Arg attribute ((unused)))
603 /* Print the assembler version */
604 {
605     fprintf (stderr, "ca65 V%s\n", GetVersionAsString ());
606 }
607
608
609
610 static void DoPCAssign (void)
611 /* Start absolute code */
612 {
613     long PC = ConstExpression ();
614     if (PC < 0 || PC > 0xFFFFFF) {
615         Error ("Range error");
616     } else {
617         EnterAbsoluteMode (PC);
618     }
619 }
620
621
622
623 static void OneLine (void)
624 /* Assemble one line */
625 {
626     Segment*      Seg   = 0;
627     unsigned long PC    = 0;
628     SymEntry*     Sym   = 0;
629     Macro*        Mac   = 0;
630     int           Instr = -1;
631
632     /* Initialize the new listing line if we are actually reading from file
633      * and not from internally pushed input.
634      */
635     if (!HavePushedInput ()) {
636         InitListingLine ();
637     }
638
639     /* Single colon means unnamed label */
640     if (CurTok.Tok == TOK_COLON) {
641         ULabDef ();
642         NextTok ();
643     }
644
645     /* If the first token on the line is an identifier, check for a macro or
646      * an instruction.
647      */
648     if (CurTok.Tok == TOK_IDENT) {
649         if (!UbiquitousIdents) {
650             /* Macros and symbols cannot use instruction names */
651             Instr = FindInstruction (&CurTok.SVal);
652             if (Instr < 0) {
653                 Mac = FindMacro (&CurTok.SVal);
654             }
655         } else {
656             /* Macros and symbols may use the names of instructions */
657             Mac = FindMacro (&CurTok.SVal);
658         }
659     }
660
661     /* Handle an identifier. This may be a cheap local symbol, or a fully
662      * scoped identifier which may start with a namespace token (for global
663      * namespace)
664      */
665     if (CurTok.Tok == TOK_LOCAL_IDENT ||
666         CurTok.Tok == TOK_NAMESPACE   ||
667         (CurTok.Tok == TOK_IDENT && Instr < 0 && Mac == 0)) {
668
669         /* Did we have whitespace before the ident? */
670         int HadWS = CurTok.WS;
671
672         /* Generate the symbol table entry, then skip the name */
673         Sym = ParseAnySymName (SYM_ALLOC_NEW);
674
675         /* If a colon follows, this is a label definition. If there
676          * is no colon, it's an assignment.
677          */
678         if (CurTok.Tok == TOK_EQ || CurTok.Tok == TOK_ASSIGN) {
679
680             /* Determine the symbol flags from the assignment token */
681             unsigned Flags = (CurTok.Tok == TOK_ASSIGN)? SF_LABEL : SF_NONE;
682
683             /* Skip the '=' */
684             NextTok ();
685
686             /* Define the symbol with the expression following the '=' */
687             SymDef (Sym, Expression(), ADDR_SIZE_DEFAULT, Flags);
688
689             /* Don't allow anything after a symbol definition */
690             ConsumeSep ();
691             return;
692
693         } else if (CurTok.Tok == TOK_SET) {
694
695             ExprNode* Expr;
696
697             /* .SET defines variables (= redefinable symbols) */
698             NextTok ();
699
700             /* Read the assignment expression, which must be constant */
701             Expr = GenLiteralExpr (ConstExpression ());
702
703             /* Define the symbol with the constant expression following
704              * the '='
705              */
706             SymDef (Sym, Expr, ADDR_SIZE_DEFAULT, SF_VAR);
707
708             /* Don't allow anything after a symbol definition */
709             ConsumeSep ();
710             return;
711
712         } else {
713
714             /* A label. Remember the current segment, so we can later
715              * determine the size of the data stored under the label.
716              */
717             Seg = ActiveSeg;
718             PC  = GetPC ();
719
720             /* Define the label */
721             SymDef (Sym, GenCurrentPC (), ADDR_SIZE_DEFAULT, SF_LABEL);
722
723             /* Skip the colon. If NoColonLabels is enabled, allow labels
724              * without a colon if there is no whitespace before the
725              * identifier.
726              */
727             if (CurTok.Tok != TOK_COLON) {
728                 if (HadWS || !NoColonLabels) {
729                     Error ("`:' expected");
730                     /* Try some smart error recovery */
731                     if (CurTok.Tok == TOK_NAMESPACE) {
732                         NextTok ();
733                     }
734                 }
735             } else {
736                 /* Skip the colon */
737                 NextTok ();
738             }
739
740             /* If we come here, a new identifier may be waiting, which may
741              * be a macro or instruction.
742              */
743             if (CurTok.Tok == TOK_IDENT) {
744                 if (!UbiquitousIdents) {
745                     /* Macros and symbols cannot use instruction names */
746                     Instr = FindInstruction (&CurTok.SVal);
747                     if (Instr < 0) {
748                         Mac = FindMacro (&CurTok.SVal);
749                     }
750                 } else {
751                     /* Macros and symbols may use the names of instructions */
752                     Mac = FindMacro (&CurTok.SVal);
753                 }
754             }
755         }
756     }
757
758     /* We've handled a possible label, now handle the remainder of the line */
759     if (CurTok.Tok >= TOK_FIRSTPSEUDO && CurTok.Tok <= TOK_LASTPSEUDO) {
760         /* A control command */
761         HandlePseudo ();
762     } else if (Mac != 0) {
763         /* A macro expansion */
764         MacExpandStart (Mac);
765     } else if (Instr >= 0 ||
766                (UbiquitousIdents && ((Instr = FindInstruction (&CurTok.SVal)) >= 0))) {
767         /* A mnemonic - assemble one instruction */
768         HandleInstruction (Instr);
769     } else if (PCAssignment && (CurTok.Tok == TOK_STAR || CurTok.Tok == TOK_PC)) {
770         NextTok ();
771         if (CurTok.Tok != TOK_EQ) {
772             Error ("`=' expected");
773             SkipUntilSep ();
774         } else {
775             /* Skip the equal sign */
776             NextTok ();
777             /* Enter absolute mode */
778             DoPCAssign ();
779         }
780     }
781
782     /* If we have defined a label, remember its size. Sym is also set by
783      * a symbol assignment, but in this case Done is false, so we don't
784      * come here.
785      */
786     if (Sym) {
787         unsigned long Size;
788         if (Seg == ActiveSeg) {
789             /* Same segment */
790             Size = GetPC () - PC;
791         } else {
792             /* The line has switched the segment */
793             Size = 0;
794         }
795         DefSizeOfSymbol (Sym, Size);
796     }
797
798     /* Line separator must come here */
799     ConsumeSep ();
800 }
801
802
803
804 static void Assemble (void)
805 /* Start the ball rolling ... */
806 {
807     /* Prime the pump */
808     NextTok ();
809
810     /* Assemble lines until end of file */
811     while (CurTok.Tok != TOK_EOF) {
812         OneLine ();
813     }
814 }
815
816
817
818 static void CreateObjFile (void)
819 /* Create the object file */
820 {
821     /* Open the object, write the header */
822     ObjOpen ();
823
824     /* Write the object file options */
825     WriteOptions ();
826
827     /* Write the list of input files */
828     WriteFiles ();
829
830     /* Write the segment data to the file */
831     WriteSegments ();
832
833     /* Write the import list */
834     WriteImports ();
835
836     /* Write the export list */
837     WriteExports ();
838
839     /* Write debug symbols if requested */
840     WriteDbgSyms ();
841
842     /* Write the scopes if requested */
843     WriteScopes ();
844
845     /* Write line infos if requested */
846     WriteLineInfos ();
847
848     /* Write the string pool */
849     WriteStrPool ();
850
851     /* Write the assertions */
852     WriteAssertions ();
853
854     /* Write the spans */
855     WriteSpans ();
856
857     /* Write an updated header and close the file */
858     ObjClose ();
859 }
860
861
862
863 int main (int argc, char* argv [])
864 /* Assembler main program */
865 {
866     /* Program long options */
867     static const LongOpt OptTab[] = {
868         { "--auto-import",      0,      OptAutoImport           },
869         { "--bin-include-dir",  1,      OptBinIncludeDir        },
870         { "--cpu",              1,      OptCPU                  },
871         { "--create-dep",       1,      OptCreateDep            },
872         { "--create-full-dep",  1,      OptCreateFullDep        },
873         { "--debug",            0,      OptDebug                },
874         { "--debug-info",       0,      OptDebugInfo            },
875         { "--feature",          1,      OptFeature              },
876         { "--help",             0,      OptHelp                 },
877         { "--ignore-case",      0,      OptIgnoreCase           },
878         { "--include-dir",      1,      OptIncludeDir           },
879         { "--large-alignment",  0,      OptLargeAlignment       },
880         { "--list-bytes",       1,      OptListBytes            },
881         { "--listing",          1,      OptListing              },
882         { "--memory-model",     1,      OptMemoryModel          },
883         { "--pagelength",       1,      OptPageLength           },
884         { "--relax-checks",     0,      OptRelaxChecks          },
885         { "--smart",            0,      OptSmart                },
886         { "--target",           1,      OptTarget               },
887         { "--verbose",          0,      OptVerbose              },
888         { "--version",          0,      OptVersion              },
889     };
890
891     /* Name of the global name space */
892     static const StrBuf GlobalNameSpace = STATIC_STRBUF_INITIALIZER;
893
894     unsigned I;
895
896     /* Initialize the cmdline module */
897     InitCmdLine (&argc, &argv, "ca65");
898
899     /* Initialize the string pool */
900     InitStrPool ();
901
902     /* Initialize the include search paths */
903     InitIncludePaths ();
904
905     /* Create the predefined segments */
906     SegInit ();
907
908     /* Enter the base lexical level. We must do that here, since we may
909      * define symbols using -D.
910      */
911     SymEnterLevel (&GlobalNameSpace, SCOPE_FILE, ADDR_SIZE_DEFAULT, 0);
912
913     /* Initialize the line infos. Must be done here, since we need line infos
914      * for symbol definitions.
915      */
916     InitLineInfo ();
917
918     /* Check the parameters */
919     I = 1;
920     while (I < ArgCount) {
921
922         /* Get the argument */
923         const char* Arg = ArgVec [I];
924
925         /* Check for an option */
926         if (Arg[0] == '-') {
927             switch (Arg[1]) {
928
929                 case '-':
930                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
931                     break;
932
933                 case 'd':
934                     OptDebug (Arg, 0);
935                     break;
936
937                 case 'g':
938                     OptDebugInfo (Arg, 0);
939                     break;
940
941                 case 'h':
942                     OptHelp (Arg, 0);
943                     break;
944
945                 case 'i':
946                     OptIgnoreCase (Arg, 0);
947                     break;
948
949                 case 'l':
950                     OptListing (Arg, GetArg (&I, 2));
951                     break;
952
953                 case 'm':
954                     if (Arg[2] == 'm') {
955                         OptMemoryModel (Arg, GetArg (&I, 3));
956                     } else {
957                         UnknownOption (Arg);
958                     }
959                     break;
960
961                 case 'o':
962                     OutFile = GetArg (&I, 2);
963                     break;
964
965                 case 's':
966                     OptSmart (Arg, 0);
967                     break;
968
969                 case 't':
970                     OptTarget (Arg, GetArg (&I, 2));
971                     break;
972
973                 case 'v':
974                     OptVerbose (Arg, 0);
975                     break;
976
977                 case 'D':
978                     DefineSymbol (GetArg (&I, 2));
979                     break;
980
981                 case 'I':
982                     OptIncludeDir (Arg, GetArg (&I, 2));
983                     break;
984
985                 case 'U':
986                     OptAutoImport (Arg, 0);
987                     break;
988
989                 case 'V':
990                     OptVersion (Arg, 0);
991                     break;
992
993                 case 'W':
994                     WarnLevel = atoi (GetArg (&I, 2));
995                     break;
996
997                 default:
998                     UnknownOption (Arg);
999                     break;
1000
1001             }
1002         } else {
1003             /* Filename. Check if we already had one */
1004             if (InFile) {
1005                 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
1006                          ProgName, Arg);
1007                 exit (EXIT_FAILURE);
1008             } else {
1009                 InFile = Arg;
1010             }
1011         }
1012
1013         /* Next argument */
1014         ++I;
1015     }
1016
1017     /* Do we have an input file? */
1018     if (InFile == 0) {
1019         fprintf (stderr, "%s: No input files\n", ProgName);
1020         exit (EXIT_FAILURE);
1021     }
1022
1023     /* Add the default include search paths. */
1024     FinishIncludePaths ();
1025
1026     /* If no CPU given, use the default CPU for the target */
1027     if (GetCPU () == CPU_UNKNOWN) {
1028         if (Target != TGT_UNKNOWN) {
1029             SetCPU (GetTargetProperties (Target)->DefaultCPU);
1030         } else {
1031             SetCPU (CPU_6502);
1032         }
1033     }
1034
1035     /* If no memory model was given, use the default */
1036     if (MemoryModel == MMODEL_UNKNOWN) {
1037         SetMemoryModel (MMODEL_NEAR);
1038     }
1039
1040     /* Set the default segment sizes according to the memory model */
1041     SetSegmentSizes ();
1042
1043     /* Initialize the scanner, open the input file */
1044     InitScanner (InFile);
1045
1046     /* Define the default options */
1047     SetOptions ();
1048
1049     /* Assemble the input */
1050     Assemble ();
1051
1052     /* If we didn't have any errors, check the pseudo insn stacks */
1053     if (ErrorCount == 0) {
1054         CheckPseudo ();
1055     }
1056
1057     /* If we didn't have any errors, check and cleanup the unnamed labels */
1058     if (ErrorCount == 0) {
1059         ULabDone ();
1060     }
1061
1062     /* If we didn't have any errors, check the symbol table */
1063     if (ErrorCount == 0) {
1064         SymCheck ();
1065     }
1066
1067     /* If we didn't have any errors, check the hll debug symbols */
1068     if (ErrorCount == 0) {
1069         DbgInfoCheck ();
1070     }
1071
1072     /* If we didn't have any errors, close the file scope lexical level */
1073     if (ErrorCount == 0) {
1074         SymLeaveLevel ();
1075     }
1076
1077     /* If we didn't have any errors, check and resolve the segment data */
1078     if (ErrorCount == 0) {
1079         SegDone ();
1080     }
1081
1082     /* If we didn't have any errors, check the assertions */
1083     if (ErrorCount == 0) {
1084         CheckAssertions ();
1085     }
1086
1087     /* Dump the data */
1088     if (Verbosity >= 2) {
1089         SymDump (stdout);
1090         SegDump ();
1091     }
1092
1093     /* If we didn't have an errors, finish off the line infos */
1094     DoneLineInfo ();
1095
1096     /* If we didn't have any errors, create the object, listing and
1097      * dependency files
1098      */
1099     if (ErrorCount == 0) {
1100         CreateObjFile ();
1101         if (SB_GetLen (&ListingName) > 0) {
1102             CreateListing ();
1103         }
1104        CreateDependencies ();
1105     }
1106
1107     /* Close the input file */
1108     DoneScanner ();
1109
1110     /* Return an apropriate exit code */
1111     return (ErrorCount == 0)? EXIT_SUCCESS : EXIT_FAILURE;
1112 }
1113
1114
1115