-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathas0.c
198 lines (181 loc) · 3.15 KB
/
as0.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
/*
* Assembler.
* Command line processing
* and main driver.
*
* FIXME: normal Unix as option parsing.
*/
#include "as.h"
#include <unistd.h>
FILE *ifp;
FILE *ofp;
FILE *lfp;
char eb[NERR];
char ib[NINPUT];
char lb[NINPUT];
char *cp;
char *ep;
char *ip;
char *fname;
char *listname;
VALUE dot[OSEG];
int segment = 1;
SYM *phash[NHASH];
SYM *uhash[NHASH];
int pass;
int line;
jmp_buf env;
int debug_write = 1 ;
int noobj;
int cpu_flags = ARCH_CPUFLAGS;
static void usage(void)
{
fprintf(stderr, "as [-o object.o] source.s.\n");
exit(1);
}
static void oom(void)
{
fprintf(stderr, "Out of memory.\n");
exit(1);
}
static char *xstrdup(const char *p)
{
char *n = strdup(p);
if (!n)
oom();
return n;
}
static int listbytes;
static void list_header(void)
{
#ifdef ADDR32
fprintf(lfp, "%1X %08X : ",
#else
fprintf(lfp, "%1X %04X : ",
#endif
#ifdef TARGET_WORD_MACHINE
segment, dot[segment] / 2);
#else
segment, dot[segment]);
#endif
listbytes = 0;
}
static void list_beginline(void)
{
if (pass !=3 || !lfp)
return;
strcpy(lb, ib); /* Save the input buffer */
list_header();
}
void list_addbyte(uint8_t byte)
{
if (pass == 3 && lfp) {
/* Deal with wrapping nicely (eg for .ascii) */
if (listbytes == 8) {
fputs(lb, lfp);
strcpy(lb, "...\n");
list_header();
}
listbytes++;
fprintf(lfp, "%02X ", byte);
}
}
void list_endline(void)
{
if (pass == 3 && lfp) {
while(listbytes++ < 8)
fputs(" ", lfp);
fputs(lb, lfp);
}
}
int main(int argc, char *argv[])
{
char *ifn;
char *ofn = NULL;
char *p, *e;
int opt;
/* Lots of options need adding yet */
while ((opt = getopt(argc, argv, "o:l:")) != -1) {
switch (opt) {
case 'o':
ofn = optarg;
break;
case 'l':
listname = optarg;
break;
default:
usage();
break;
}
}
if (optind != argc - 1)
usage();
ifn = argv[optind];
if ((ifp=fopen(ifn, "r")) == NULL) {
fprintf(stderr, "%s: cannot open\n", ifn);
exit(BAD);
}
if (ofn == NULL) {
ofn = xstrdup(ifn);
p = strrchr(ofn, '.');
if (p == NULL || p[1] == 0) {
fprintf(stderr, "%s: expected extensions.\n", ifn);
exit(BAD);
}
p[1] = 'o';
p[2] = '\0';
}
if ((ofp=fopen(ofn, "w")) == NULL) {
fprintf(stderr, "%s: cannot create.\n", ofn);
exit(BAD);
}
if (listname) {
lfp = fopen(listname, "w");
if (lfp == NULL) {
fprintf(stderr, "%s: cannot create.\n", listname);
exit(BAD);
}
}
syminit();
fname = xstrdup(ifn);
for (pass=0; pass<4; ++pass) {
if (outpass() == 0)
continue;
line = 1;
memset(dot, 0, sizeof(dot));
fseek(ifp, 0L, 0);
while (fgets(ib, NINPUT, ifp) != NULL) {
/* Pre-processor output */
if (*ib == '#' && ib[1] == ' ') {
free(fname);
line = strtoul(ib +2, &p, 10);
p++;
e = strrchr(p , '"');
if (e)
*e = 0;
fname = xstrdup(p);
/* Normal assembly */
} else {
list_beginline();
ep = &eb[0];
ip = &ib[0];
if (setjmp(env) == 0)
asmline();
list_endline();
++line;
}
}
/* Don't continue once we know we failed */
if (noobj)
break;
}
if (!noobj) {
pass = 3;
outeof();
} else {
if (unlink(ofn))
perror(ofn);
}
/* Return an error code if no object was created */
exit(noobj);
}