comparison src/list.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 * list.c
3 *
4 * code for displaying a program listing in lwasm
5 */
6
7 //#include <ctype.h>
8 #include <errno.h>
9 #include <stdio.h>
10 //#include <stdlib.h>
11 #include <string.h>
12 #define __list_c_seen__
13 //#include "instab.h"
14 #include "lwasm.h"
15
16 const char *errlist[] =
17 {
18 "No error",
19 "Bad opcode",
20 "Illegal Symbol",
21 "Multiply defined symbol",
22 "Symbol required but not present",
23 "Forward references not permitted",
24 "Byte overflow",
25 "Phase error",
26 "Bad operand",
27 "Symbol not permitted here",
28 "Undefined symbol",
29 "Bit number out of range",
30 "Invalid expression",
31 "Invalid register",
32 "Bad file name",
33 "ENDM without MACRO",
34 "Redefined macro",
35 "Nested namespace",
36 "Bad condition",
37 "User error",
38 "Bad pragma",
39 ""
40 };
41
42 void list_code(asmstate_t *as)
43 {
44 FILE *lf;
45 sourceline_t *cl;
46 int bn;
47 int c;
48 char *t;
49
50 if (as -> listfile && strcmp(as -> listfile, "-"))
51 {
52 lf = fopen(as -> listfile, "w");
53 if (!lf)
54 {
55 perror("Cannot open list file");
56 return;
57 }
58 }
59 else
60 {
61 lf = stdout;
62 }
63
64 for (cl = as -> source_head; cl; cl = cl -> next)
65 {
66 bn = 0;
67 if (cl -> errors)
68 {
69 errortab_t *e;
70 for (e = cl -> errors; e; e = e -> next)
71 {
72 if (e -> errnum < ERR_MAXERR && e -> errnum != ERR_USER)
73 fprintf(lf, "*****ERROR: %s\n", errlist[e -> errnum]);
74 }
75 if (cl -> user_error)
76 {
77 fprintf(lf, "*****ERROR: %s\n", cl -> user_error);
78 }
79 }
80 if (cl -> skipped)
81 {
82 fprintf(lf, "%-15.15s", "<skipped>");
83 }
84 else if (cl -> macrodef)
85 {
86 fprintf(lf, "%-15.15s", "<macrodef>");
87 }
88 else if (cl -> opcode >= 0 && cl -> numcodebytes > 0)
89 {
90 fprintf(lf, "%04X ", cl -> addr);
91 while (bn < 5 && bn < cl -> numcodebytes)
92 {
93 fprintf(lf, "%02X", cl -> codebytes[bn]);
94 bn++;
95 }
96 while (bn < 5)
97 {
98 fprintf(lf, " ");
99 bn++;
100 }
101 }
102 else if (cl -> addrset || (cl -> len && cl -> numcodebytes == 0))
103 {
104 fprintf(lf, "%04X %10s", cl -> addr, "");
105 }
106 else if (cl -> isequ)
107 {
108 fprintf(lf, " %04X ", cl -> symaddr);
109 }
110 else if (cl -> issetdp)
111 {
112 fprintf(lf, " %02X ", cl -> dpval);
113 }
114 else
115 fprintf(lf, " ");
116 fprintf(lf, " %15.15s:%06d ", cl -> sourcefile, cl -> lineno);
117 // actually display the line from the file
118 for (c = 0, t = cl -> line; *t; t++)
119 {
120 if (*t == '\n' || *t == '\r')
121 break;
122 if (*t == '\t')
123 {
124 do
125 {
126 fprintf(lf, " ");
127 c++;
128 } while (c % 8);
129 }
130 else
131 {
132 c++;
133 fprintf(lf, "%c", *t);
134 }
135 }
136 // fprintf(lf, "\n");
137
138 while (bn < cl -> numcodebytes)
139 {
140 if (bn % 5 == 0)
141 fprintf(lf, "\n%04X ", (cl -> addr + bn) & 0xFFFF);
142 fprintf(lf, "%02X", cl -> codebytes[bn]);
143 bn++;
144 }
145 fprintf(lf, "\n");
146 }
147
148 fprintf(lf, "\n");
149 list_symbols(as, lf);
150
151 if (lf != stdout)
152 fclose(lf);
153 }
154