forked from shattered/macro11
-
Notifications
You must be signed in to change notification settings - Fork 5
/
listing.c
198 lines (153 loc) · 4.62 KB
/
listing.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
#define LISTING__C
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdarg.h>
#include "listing.h" /* my own definitions */
#include "util.h"
#include "assemble_globals.h"
/* GLOBAL VARIABLES */
int list_md = 1; /* option to list macro/rept definition = yes */
int list_me = 1; /* option to list macro/rept expansion = yes */
int list_bex = 1; /* option to show binary */
int list_level = 1; /* Listing control level. .LIST
increments; .NLIST decrements */
static char *listline; /* Source lines */
static char *binline; /* for octal expansion */
FILE *lstfile = NULL;
int list_pass_0 = 0;/* Also list what happens during the first pass */
static int errline = 0; /* Set if current line has an error */
/* maybe_list returns TRUE if listing may happen for this line. */
static int can_list(
void)
{
int ok = lstfile != NULL &&
(pass > 0 || list_pass_0);
return ok;
}
/* do_list returns TRUE if listing is enabled. */
static int dolist(
void)
{
int ok = can_list () &&
(list_level > 0 || errline);
return ok;
}
/* list_source saves a text line for later listing by list_flush */
void list_source(
STREAM *str,
char *cp)
{
if (can_list()) {
int len = strcspn(cp, "\n");
/* Not an error yet */
errline = 0;
/* Save the line text away for later... */
if (listline)
free(listline);
listline = memcheck(malloc(len + 1));
memcpy(listline, cp, len);
listline[len] = 0;
if (!binline)
binline = memcheck(malloc(sizeof(LSTFORMAT) + 16));
sprintf(binline, "%*s%*d", (int)SIZEOF_MEMBER(LSTFORMAT, flag), "", (int)SIZEOF_MEMBER(LSTFORMAT, line_number),
str->line);
}
}
/* list_flush produces a buffered list line. */
void list_flush(
void)
{
if (dolist()) {
padto(binline, offsetof(LSTFORMAT, source));
fputs(binline, lstfile);
fputs(listline, lstfile);
fputc('\n', lstfile);
listline[0] = 0;
binline[0] = 0;
}
}
/* list_fit checks to see if a word will fit in the current listing
line. If not, it flushes and prepares another line. */
static void list_fit(
STREAM *str,
unsigned addr)
{
size_t col1 = offsetof(LSTFORMAT, source);
size_t col2 = offsetof(LSTFORMAT, pc);
if (strlen(binline) >= col1) {
list_flush();
listline[0] = 0;
binline[0] = 0;
sprintf(binline, "%*s %6.6o", (int)offsetof(LSTFORMAT, pc), "", addr);
padto(binline, offsetof(LSTFORMAT, words));
} else if (strlen(binline) <= col2) {
sprintf(binline, "%*s%*d %6.6o", (int)SIZEOF_MEMBER(LSTFORMAT, flag), "",
(int)SIZEOF_MEMBER(LSTFORMAT, line_number), str->line, addr);
padto(binline, offsetof(LSTFORMAT, words));
}
}
/* list_value is used to show a computed value */
void list_value(
STREAM *str,
unsigned word)
{
if (dolist()) {
/* Print the value and go */
binline[0] = 0;
sprintf(binline, "%*s%*d %6.6o", (int)SIZEOF_MEMBER(LSTFORMAT, flag), "",
(int)SIZEOF_MEMBER(LSTFORMAT, line_number), str->line, word & 0177777);
}
}
/* Print a word to the listing file */
void list_word(
STREAM *str,
unsigned addr,
unsigned value,
int size,
char *flags)
{
if (dolist()) {
list_fit(str, addr);
if (size == 1)
sprintf(binline + strlen(binline), " %3.3o%1.1s ", value & 0377, flags);
else
sprintf(binline + strlen(binline), "%6.6o%1.1s ", value & 0177777, flags);
}
}
/* Print just a line with the address to the listing file */
void list_location(
STREAM *str,
unsigned addr)
{
if (dolist()) {
list_fit(str, addr);
}
}
/* reports errors */
void report(
STREAM *str,
char *fmt,
...)
{
va_list ap;
char *name = "**";
int line = 0;
if (!pass && list_pass_0 < 2)
return; /* Don't report now. */
errline = 1;
if (str) {
name = str->name;
line = str->line;
}
fprintf(stderr, "%s:%d: ***ERROR ", name, line);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
if (lstfile) {
fprintf(lstfile, "%s:%d: ***ERROR ", name, line);
va_start(ap, fmt);
vfprintf(lstfile, fmt, ap);
va_end(ap);
}
}