annotate src/main.c @ 0:57495da01900

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