Debian Codegraph @

src/asm/output.c

raw symbols SCIP
1 //
2 // ACME - a crossassembler for producing 6502/65c02/65816 code.
3 // Copyright (C) 1998-2006 Marco Baye
4 // Have a look at "acme.c" for further info
5 //
6 // Output stuff
7
8 #include <stdlib.h>
9 //#include <stdio.h>
10 #include <string.h> // for memset()
11 #include "acme.h"
12 #include "alu.h"
13 #include "config.h"
14 #include "cpu.h"
15 #include "dynabuf.h"
16 #include "global.h"
17 #include "input.h"
18 #include "output.h"
19 #include "platform.h"
20 #include "tree.h"
21
22
23 // Structure for linked list of segment data
24 struct segment_t {
25 struct segment_t* next;
26 // struct segment_t* prev;
27 intval_t start;
28 intval_t length;
29 };
30
31
32 // Constants
33 #define OUTBUFFERSIZE 65536
34 enum out_format_t {
35 OUTPUT_FORMAT_UNSPECIFIED, // default (uses "plain" actually)
36 OUTPUT_FORMAT_PLAIN,
37 OUTPUT_FORMAT_CBM, // default for "!to" pseudo opcode
38 };
39
40
41 // Variables
42
43 // segment stuff
44 static struct segment_t* segment_list = NULL;// linked list
45 static intval_t segment_start;// Start of current segment
46 static intval_t segment_max;// Highest address segment may use
47 // misc
48 static intval_t lowest_idx; // Smallest address program uses
49 static intval_t highest_idx; // End address of program plus one
50 // output buffer stuff
51 static char* output_buffer = NULL; // to hold assembled code
52 static char* write_ptr = NULL; // points into output_buffer
53 intval_t write_idx; // index in output buffer
54 static bool memory_initialised = FALSE;
55 // chosen file format
56 static enum out_format_t output_format = OUTPUT_FORMAT_CBM;
57 // predefined stuff
58 static node_t* file_format_tree = NULL;// tree to hold output formats
59 // possible file formats
60 static node_t file_formats[] = {
61 PREDEFNODE(s_cbm, OUTPUT_FORMAT_CBM),
62 // PREDEFNODE("o65", OUTPUT_FORMAT_O65),
63 PREDEFLAST("plain", OUTPUT_FORMAT_PLAIN),
64 // ^^^^ this marks the last element
65 };
66
67
68 // Functions
69
70 // Fill output buffer with given byte value
71 static void fill_completely(char value) {
72 memset(output_buffer, value, OUTBUFFERSIZE);
73 }
74
75 // Set up new segment_max value according to the given address. Just
76 // find the next segment start and subtract 1.
77 static void find_segment_max(intval_t new_pc) {
78 struct segment_t* test_segment;
79
80 // may be faster if list is ordered!
81 segment_max = OUTBUFFERSIZE;// will be decremented later!
82 test_segment = segment_list;
83 while(test_segment) {
84 if((test_segment->start > new_pc)
85 && (test_segment->start < segment_max))
86 segment_max = test_segment->start;
87 test_segment = test_segment->next;
88 }
89 segment_max--;// last free address available
90 }
91
92 //
93 static void border_crossed(int current_offset) {
94 if(current_offset >= OUTBUFFERSIZE)
95 Throw_serious_error("Produced too much code.");
96 if(pass_count == 0) {
97 Throw_warning("Segment reached another one, overwriting it.");
98 find_segment_max(current_offset + 1);
99 }
100 }
101
102 // Send low byte to output buffer
103 void (*Output_byte)(intval_t byte);
104 static void real_output(intval_t byte); // fn for actual output
105 static void no_output(intval_t byte); // fn when output impossible
106
107 // set output pointer (negative values deactivate output)
108 static void set_mem_ptr(signed long index) {
109 if(index < 0) {
110 Output_byte = no_output;
111 write_ptr = output_buffer;
112 write_idx = 0;
113 } else {
114 Output_byte = real_output;
115 write_ptr = output_buffer + index;
116 write_idx = index;
117 }
118 }
119
120 // Send low byte to output buffer, automatically increasing program counter
121 static void real_output(intval_t byte) {
122 if(write_idx > segment_max)
123 border_crossed(write_idx);
124 *write_ptr++ = byte & 0xff;
125 write_idx++;
126 CPU_2add++;
127 }
128
129 // fail to write to output buffer
130 static void no_output(intval_t byte) {
131 Throw_error(exception_pc_undefined);
132 // set ptr to not complain again. as we have thrown an error, assembly
133 // fails, so don't care about actual value.
134 set_mem_ptr(512); // 512 to not garble zero page and stack. ;)
135 CPU_2add++;
136 }
137
138 // call this if really calling Output_byte would be a waste of time
139 void Output_fake(int size) {
140 // check whether ptr undefined
141 if(Output_byte == no_output) {
142 no_output(0);
143 size--;
144 }
145 if(write_idx + size - 1 > segment_max)
146 border_crossed(write_idx + size - 1);
147 write_ptr += size;
148 write_idx += size;
149 CPU_2add += size;
150 }
151
152 // Output 8-bit value with range check
153 void Output_8b(intval_t value) {
154 if((value <= 0xff) && (value >= -0x80))
155 Output_byte(value);
156 else
157 Throw_error(exception_number_out_of_range);
158 }
159
160 // Output 16-bit value with range check
161 void Output_16b(intval_t value) {
162 if((value <= 0xffff) && (value >= -0x8000)) {
163 Output_byte(value);
164 Output_byte(value >> 8);
165 } else
166 Throw_error(exception_number_out_of_range);
167 }
168
169 // Output 24-bit value with range check
170 void Output_24b(intval_t value) {
171 if((value <= 0xffffff) && (value >= -0x800000)) {
172 Output_byte(value);
173 Output_byte(value >> 8);
174 Output_byte(value >> 16);
175 } else
176 Throw_error(exception_number_out_of_range);
177 }
178
179 // Output 32-bit value (without range check)
180 void Output_32b(intval_t value) {
181 // if((Value <= 0x7fffffff) && (Value >= -0x80000000)) {
182 Output_byte(value);
183 Output_byte(value >> 8);
184 Output_byte(value >> 16);
185 Output_byte(value >> 24);
186 // } else
187 // Throw_error(exception_number_out_of_range);
188 }
189
190 // Define default value for empty memory ("!initmem" pseudo opcode)
191 static enum eos_t PO_initmem(void) {
192 intval_t content;
193
194 // ignore in all passes but in first
195 if(pass_count)
196 return(SKIP_REMAINDER);
197 // if MemInit flag is already set, complain
198 if(memory_initialised) {
199 Throw_warning("Memory already initialised.");
200 return(SKIP_REMAINDER);
201 }
202 // set MemInit flag
203 memory_initialised = TRUE;
204 // get value and init memory
205 content = ALU_defined_int();
206 if((content > 0xff) || (content < -0x80))
207 Throw_error(exception_number_out_of_range);
208 // init memory
209 fill_completely(content);
210 // enforce another pass
211 if(pass_undefined_count == 0)
212 pass_undefined_count = 1;
213 // FIXME - enforcing another pass is not needed if there hasn't been any
214 // output yet. But that's tricky to detect without too much overhead.
215 // The old solution was to add &&(lowest_idx < highest_idx) to "if" above
216 return(ENSURE_EOS);
217 }
218
219 // Try to set output format held in DynaBuf. Returns whether succeeded.
220 bool Output_set_output_format(void) {
221 void* node_body;
222
223 if(!Tree_easy_scan(file_format_tree, &node_body, GlobalDynaBuf))
224 return(FALSE);
225 output_format = (enum out_format_t) node_body;
226 return(TRUE);
227 }
228
229 // Select output file and format ("!to" pseudo opcode)
230 static enum eos_t PO_to(void) {
231 // only act upon this pseudo opcode in first pass
232 if(pass_count)
233 return(SKIP_REMAINDER);
234 // if output file already chosen, complain and exit
235 if(output_filename) {
236 Throw_warning("Output file already chosen.");
237 return(SKIP_REMAINDER);
238 }
239 // read filename to global dynamic buffer
240 // if no file name given, exit (complaining will have been done)
241 if(Input_read_filename(FALSE))
242 return(SKIP_REMAINDER);
243 // get malloc'd copy of filename
244 output_filename = DynaBuf_get_copy(GlobalDynaBuf);
245 // select output format
246 // if no comma found, use default file format
247 if(Input_accept_comma() == FALSE) {
248 if(output_format == OUTPUT_FORMAT_UNSPECIFIED) {
249 output_format = OUTPUT_FORMAT_CBM;
250 // output deprecation warning
251 Throw_warning("Used \"!to\" without file format indicator. Defaulting to \"cbm\".");
252 }
253 return(ENSURE_EOS);
254 }
255 // parse output format name
256 // if no keyword given, give up
257 if(Input_read_and_lower_keyword() == 0)
258 return(SKIP_REMAINDER);
259 if(Output_set_output_format())
260 return(ENSURE_EOS); // success
261 // error occurred
262 Throw_error("Unknown output format.");
263 return(SKIP_REMAINDER);
264 }
265
266 // pseudo ocpode table
267 static node_t pseudo_opcodes[] = {
268 PREDEFNODE("initmem", PO_initmem),
269 PREDEFLAST("to", PO_to),
270 // ^^^^ this marks the last element
271 };
272
273 // Init file format tree (is done early)
274 void Outputfile_init(void) {
275 Tree_add_table(&file_format_tree, file_formats);
276 }
277
278 // alloc and init mem buffer, register pseudo opcodes (done later)
279 void Output_init(signed long fill_value) {
280 output_buffer = safe_malloc(OUTBUFFERSIZE);
281 write_ptr = output_buffer;
282 if(fill_value == MEMINIT_USE_DEFAULT)
283 fill_value = FILLVALUE_INITIAL;
284 else
285 memory_initialised = TRUE;
286 // init output buffer (fill memory with initial value)
287 fill_completely(fill_value & 0xff);
288 Tree_add_table(&pseudo_opcode_tree, pseudo_opcodes);
289 }
290
291 // Dump memory buffer into output file
292 void Output_save_file(FILE* fd) {
293 intval_t amount = highest_idx - lowest_idx;
294
295 if(Process_verbosity)
296 printf("Saving %ld ($%lx) bytes ($%lx - $%lx exclusive).\n",
297 amount, amount, lowest_idx, highest_idx);
298 // Output file header according to file format
299 switch(output_format) {
300
301 case OUTPUT_FORMAT_UNSPECIFIED:
302 case OUTPUT_FORMAT_PLAIN:
303 PLATFORM_SETFILETYPE_PLAIN(output_filename);
304 break;
305
306 case OUTPUT_FORMAT_CBM:
307 PLATFORM_SETFILETYPE_CBM(output_filename);
308 // output 16-bit load address in little-endian byte order
309 putc(lowest_idx & 255, fd);
310 putc(lowest_idx >> 8, fd);
311 break;
312
313 }
314 // Dump output buffer to file
315 fwrite(output_buffer + lowest_idx, sizeof(char), amount, fd);
316 }
317
318 char* Output_get_final_data(int *length) {
319 intval_t amount = highest_idx - lowest_idx;
320 int bufsize = amount * sizeof(char);
321 if(Process_verbosity)
322 printf("Saving %ld ($%lx) bytes ($%lx - $%lx exclusive).\n",
323 amount, amount, lowest_idx, highest_idx);
324 // Output file header according to file format
325 switch(output_format) {
326
327 case OUTPUT_FORMAT_UNSPECIFIED:
328 case OUTPUT_FORMAT_PLAIN:
329 PLATFORM_SETFILETYPE_PLAIN(output_filename);
330 break;
331
332 case OUTPUT_FORMAT_CBM:
333 PLATFORM_SETFILETYPE_CBM(output_filename);
334
335 // TODO: insert load address here
336 // output 16-bit load address in little-endian byte order
337
338 //cbm_load_address = lowest_idx;
339 //putc(lowest_idx & 255, fd);
340 //putc(lowest_idx >> 8, fd);
341 bufsize += 2;
342 break;
343
344 }
345 // Dump output buffer to file
346 //fwrite(output_buffer + lowest_idx, sizeof(char), amount, fd);
347 char* buffer = malloc(bufsize);
348 if(output_format == OUTPUT_FORMAT_CBM) {
349 buffer[0] = lowest_idx & 255;
350 buffer[1] = lowest_idx >> 8;
351 memcpy(buffer + 2, output_buffer + lowest_idx, amount);
352 }
353 else {
354 memcpy(buffer, output_buffer + lowest_idx, amount);
355 }
356 *length = bufsize;
357 return buffer;
358 }
359
360
361 // Link segment data into segment chain
362 static void link_segment(intval_t start, intval_t length) {
363 struct segment_t* new_segment;
364
365 new_segment = safe_malloc(sizeof(struct segment_t));
366 new_segment->start = start;
367 new_segment->length = length;
368 new_segment->next = segment_list;
369 segment_list = new_segment;
370 }
371
372 // Show start and end of current segment
373 // Called whenever a new segment begins, and at end of pass.
374 void Output_end_segment(void) {
375 intval_t amount;
376
377 if(CPU_uses_pseudo_pc())
378 Throw_first_pass_warning("Offset assembly still active at end of segment."); // FIXME - should be error!
379 if(pass_count == 0) {
380 amount = write_idx - segment_start;
381 link_segment(segment_start, amount);
382 if(Process_verbosity > 1)
383 printf(
384 "Segment size is %ld ($%lx) bytes ($%lx - $%lx exclusive).\n",
385 amount, amount, segment_start, write_idx);
386 }
387
388 // FIXME - this was in real_output():
389 // check for new max address (FIXME - move to close_segment?)
390 if(write_idx > highest_idx)
391 highest_idx = write_idx;
392 }
393
394 // Check whether given PC is inside segment.
395 static void check_segment(intval_t new_pc) {
396 struct segment_t* test_segment;
397
398 test_segment = segment_list;
399 while(test_segment) {
400 if((new_pc >= test_segment->start)
401 && (new_pc < (test_segment->start) + (test_segment->length)))
402 Throw_warning("Segment starts inside another one, overwriting it.");
403 test_segment = test_segment->next;
404 }
405 }
406
407 // init lowest and highest address
408 static void init_borders(intval_t address) {
409 lowest_idx = address;
410 highest_idx = address;
411 }
412
413 // clear segment list
414 void Output_passinit(signed long start_addr) {
415 struct segment_t* temp;
416
417 // delete segment list (and free blocks)
418 while((temp = segment_list)) {
419 segment_list = segment_list->next;
420 free(temp);
421 }
422 set_mem_ptr(start_addr); // negative values deactivate output
423 // if start address given, set program counter
424 if(start_addr >= 0) {
425 CPU_set_pc(start_addr);
426 // FIXME - integrate next two?
427 init_borders(start_addr);
428 segment_start = start_addr;
429 } else {
430 init_borders(0); // set to _something_ (for !initmem)
431 segment_start = 0;
432 }
433 // other stuff
434 segment_max = OUTBUFFERSIZE-1;
435 }
436
437 // Called when "*=EXPRESSION" is parsed
438 void Output_start_segment(void) {
439 intval_t new_addr = ALU_defined_int();
440
441 if(CPU_pc.flags & MVALUE_DEFINED) {
442 // It's a redefinition. Check some things:
443 // check whether new low
444 if(new_addr < lowest_idx)
445 lowest_idx = new_addr;
446 // show status of previous segment
447 Output_end_segment();
448 // in first pass, maybe issue warning
449 if(pass_count == 0) {
450 check_segment(new_addr);
451 find_segment_max(new_addr);
452 }
453 } else
454 init_borders(new_addr);// it's the first pc definition
455 CPU_set_pc(new_addr);
456 segment_start = new_addr;
457 set_mem_ptr(new_addr);
458 }