comparison src/output.c @ 48:6de358e7903f

Cleaned up warnings about the return value of fwrite() being ignored (it still is but now there's no warning)
author lost
date Sun, 04 Jan 2009 07:31:20 +0000
parents b962cee20bf4
children 918be0c02239
comparison
equal deleted inserted replaced
47:804d7465e0f9 48:6de358e7903f
32 #include "lwasm.h" 32 #include "lwasm.h"
33 33
34 void write_code_raw(asmstate_t *as, FILE *of); 34 void write_code_raw(asmstate_t *as, FILE *of);
35 void write_code_decb(asmstate_t *as, FILE *of); 35 void write_code_decb(asmstate_t *as, FILE *of);
36 void write_code_rawrel(asmstate_t *as, FILE *of); 36 void write_code_rawrel(asmstate_t *as, FILE *of);
37
38 // this prevents warnings about not using the return value of fwrite()
39 #define writebytes(s, l, c, f) do { int r; r = fwrite((s), (l), (c), (f)); } while (0)
37 40
38 void lwasm_output(asmstate_t *as) 41 void lwasm_output(asmstate_t *as)
39 { 42 {
40 FILE *of; 43 FILE *of;
41 44
94 { 97 {
95 if (cl -> codelen == 0) 98 if (cl -> codelen == 0)
96 continue; 99 continue;
97 100
98 fseek(of, cl -> codeaddr, SEEK_SET); 101 fseek(of, cl -> codeaddr, SEEK_SET);
99 fwrite(cl -> bytes, cl -> codelen, 1, of); 102 writebytes(cl -> bytes, cl -> codelen, 1, of);
100 } 103 }
101 } 104 }
102 105
103 /* 106 /*
104 raw merely writes all the bytes directly to the file as is. ORG is just a 107 raw merely writes all the bytes directly to the file as is. ORG is just a
113 { 116 {
114 if (cl -> nocodelen) 117 if (cl -> nocodelen)
115 { 118 {
116 int i; 119 int i;
117 for (i = 0; i < cl -> nocodelen; i++) 120 for (i = 0; i < cl -> nocodelen; i++)
118 fwrite("\0", 1, 1, of); 121 writebytes("\0", 1, 1, of);
119 continue; 122 continue;
120 } 123 }
121 fwrite(cl -> bytes, cl -> codelen, 1, of); 124 writebytes(cl -> bytes, cl -> codelen, 1, of);
122 } 125 }
123 } 126 }
124 127
125 void write_code_decb(asmstate_t *as, FILE *of) 128 void write_code_decb(asmstate_t *as, FILE *of)
126 { 129 {
141 { 144 {
142 // update previous preamble if needed 145 // update previous preamble if needed
143 fseek(of, preambloc, SEEK_SET); 146 fseek(of, preambloc, SEEK_SET);
144 outbuf[0] = (blocklen >> 8) & 0xFF; 147 outbuf[0] = (blocklen >> 8) & 0xFF;
145 outbuf[1] = blocklen & 0xFF; 148 outbuf[1] = blocklen & 0xFF;
146 fwrite(outbuf, 2, 1, of); 149 writebytes(outbuf, 2, 1, of);
147 fseek(of, 0, SEEK_END); 150 fseek(of, 0, SEEK_END);
148 } 151 }
149 blocklen = 0; 152 blocklen = 0;
150 nextcalc = cl -> codeaddr; 153 nextcalc = cl -> codeaddr;
151 outbuf[0] = 0x00; 154 outbuf[0] = 0x00;
152 outbuf[1] = 0x00; 155 outbuf[1] = 0x00;
153 outbuf[2] = 0x00; 156 outbuf[2] = 0x00;
154 outbuf[3] = (nextcalc >> 8) & 0xFF; 157 outbuf[3] = (nextcalc >> 8) & 0xFF;
155 outbuf[4] = nextcalc & 0xFF; 158 outbuf[4] = nextcalc & 0xFF;
156 preambloc = ftell(of) + 1; 159 preambloc = ftell(of) + 1;
157 fwrite(outbuf, 5, 1, of); 160 writebytes(outbuf, 5, 1, of);
158 } 161 }
159 nextcalc += cl -> codelen; 162 nextcalc += cl -> codelen;
160 fwrite(cl -> bytes, cl -> codelen, 1, of); 163 writebytes(cl -> bytes, cl -> codelen, 1, of);
161 blocklen += cl -> codelen; 164 blocklen += cl -> codelen;
162 } 165 }
163 if (blocklen > 0) 166 if (blocklen > 0)
164 { 167 {
165 fseek(of, preambloc, SEEK_SET); 168 fseek(of, preambloc, SEEK_SET);
166 outbuf[0] = (blocklen >> 8) & 0xFF; 169 outbuf[0] = (blocklen >> 8) & 0xFF;
167 outbuf[1] = blocklen & 0xFF; 170 outbuf[1] = blocklen & 0xFF;
168 fwrite(outbuf, 2, 1, of); 171 writebytes(outbuf, 2, 1, of);
169 fseek(of, 0, SEEK_END); 172 fseek(of, 0, SEEK_END);
170 } 173 }
171 174
172 // now write postamble 175 // now write postamble
173 outbuf[0] = 0xFF; 176 outbuf[0] = 0xFF;
174 outbuf[1] = 0x00; 177 outbuf[1] = 0x00;
175 outbuf[2] = 0x00; 178 outbuf[2] = 0x00;
176 outbuf[3] = (as -> execaddr >> 8) & 0xFF; 179 outbuf[3] = (as -> execaddr >> 8) & 0xFF;
177 outbuf[4] = (as -> execaddr) & 0xFF; 180 outbuf[4] = (as -> execaddr) & 0xFF;
178 fwrite(outbuf, 5, 1, of); 181 writebytes(outbuf, 5, 1, of);
179 } 182 }