comparison src/main.c @ 0:57495da01900

Initial checking of LWASM
author lost
date Fri, 03 Oct 2008 02:44:20 +0000
parents
children 34568fab6058
comparison
equal deleted inserted replaced
-1:000000000000 0:57495da01900
1 /*
2 main.c
3
4 Implements the program startup code
5
6 */
7
8 #include <argp.h>
9 #include <errno.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 #include "lwasm.h"
14
15 // external declarations
16 extern void resolve_phasing(asmstate_t *as);
17 extern void generate_code(asmstate_t *as);
18 extern void list_code(asmstate_t *as);
19 extern void write_code(asmstate_t *as);
20
21
22 // command line option handling
23 const char *argp_program_version = "LWASM Version 0.0";
24 const char *argp_program_bug_address = "lost@l-w.ca";
25
26 static error_t parse_opts(int key, char *arg, struct argp_state *state)
27 {
28 asmstate_t *as = state -> input;
29
30 switch (key)
31 {
32 case 'o':
33 // output
34 if (as -> outfile)
35 {
36 }
37 as -> outfile = arg;
38 break;
39
40 case 'd':
41 // debug
42 as -> debug++;
43 break;
44
45 case 'l':
46 // list
47 if (arg)
48 as -> listfile = arg;
49 else
50 as -> listfile = "-";
51 break;
52
53 case 'b':
54 // decb output
55 as -> outformat = OUTPUT_DECB;
56 break;
57
58 case 'r':
59 // raw binary output
60 as -> outformat = OUTPUT_RAW;
61 break;
62
63 case ARGP_KEY_END:
64 // done; sanity check
65 if (!as -> outfile)
66 as -> outfile = "a.out";
67 break;
68
69 case ARGP_KEY_ARG:
70 // non-option arg
71 if (as -> infile)
72 argp_usage(state);
73 as -> infile = arg;
74 break;
75
76 default:
77 return ARGP_ERR_UNKNOWN;
78 }
79 return 0;
80 }
81
82 static struct argp_option options[] =
83 {
84 { "output", 'o', "FILE", 0,
85 "Output to FILE"},
86 { "debug", 'd', 0, 0,
87 "Set debug mode"},
88 { "list", 'l', "FILE", OPTION_ARG_OPTIONAL,
89 "Generate list [to FILE]"},
90 { "decb", 'b', 0, 0,
91 "Generate DECB .bin format output"},
92 { "raw", 'r', 0, 0,
93 "Generate raw binary format output"},
94 { "rawrel", 0, 0, 0,
95 "Generate raw binary respecing ORG statements as offsets from the start of the file"},
96 { 0 }
97 };
98
99 static struct argp argp =
100 {
101 options,
102 parse_opts,
103 "<input file>",
104 "LWASM, a HD6309 and MC6809 cross-assembler"
105 };
106
107 // main function; parse command line, set up assembler state, and run the
108 // assembler on the first file
109 int main(int argc, char **argv)
110 {
111 // assembler state
112 asmstate_t asmstate = { 0 };
113
114 argp_parse(&argp, argc, argv, 0, 0, &asmstate);
115 if (!asmstate.listfile)
116 asmstate.listfile = "-";
117
118 // printf("Assembling %s to %s; list to %s\n", asmstate.infile, asmstate.outfile, asmstate.listfile);
119
120 /* pass 1 - collect the symbols and assign addresses where possible */
121 /* pass 1 also resolves included files, etc. */
122 /* that means files are read exactly once unless included multiple times */
123 lwasm_read_file(&asmstate, (char *)(asmstate.infile));
124
125 // pass 2: actually generate the code; if any phasing errors appear
126 // at this stage, we have a bug
127 generate_code(&asmstate);
128
129 // now make a pretty listing
130 list_code(&asmstate);
131
132 // now write the code out to the output file
133 write_code(&asmstate);
134
135 if (asmstate.errorcount > 0)
136 exit(1);
137
138 exit(0);
139 }
140