| 1 | //cxx . . $ `<file>/src/asm/output.c`/ |
| 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"cxx . . $ `<file>/src/asm/acme.h`/ |
| 12 | #include "alu.h"cxx . . $ `<file>/src/asm/alu.h`/ |
| 13 | #include "config.h"cxx . . $ `<file>/src/asm/config.h`/ |
| 14 | #include "cpu.h"cxx . . $ `<file>/src/asm/cpu.h`/ |
| 15 | #include "dynabuf.h"cxx . . $ `<file>/src/asm/dynabuf.h`/ |
| 16 | #include "global.h"cxx . . $ `<file>/src/asm/global.h`/ |
| 17 | #include "input.h"cxx . . $ `<file>/src/asm/input.h`/ |
| 18 | #include "output.h"cxx . . $ `<file>/src/asm/output.h`/ |
| 19 | #include "platform.h"cxx . . $ `<file>/src/asm/platform.h`/ |
| 20 | #include "tree.h"cxx . . $ `<file>/src/asm/tree.h`/ |
| 21 | |
| 22 | |
| 23 | // Structure for linked list of segment data |
| 24 | struct segment_tcxx . . $ segment_t# { |
| 25 | struct segment_tcxx . . $ segment_t#* nextcxx . . $ segment_t#next.; |
| 26 | // struct segment_t* prev; |
| 27 | intval_tcxx . . $ intval_t# startcxx . . $ segment_t#start.; |
| 28 | intval_tcxx . . $ intval_t# lengthcxx . . $ segment_t#length.; |
| 29 | }; |
| 30 | |
| 31 | |
| 32 | // Constants |
| 33 | #define OUTBUFFERSIZEcxx . . $ `src/asm/output.c:33:9`! 65536 |
| 34 | enum out_format_tcxx . . $ out_format_t# { |
| 35 | OUTPUT_FORMAT_UNSPECIFIEDcxx . . $ OUTPUT_FORMAT_UNSPECIFIED., // default (uses "plain" actually) |
| 36 | OUTPUT_FORMAT_PLAINcxx . . $ OUTPUT_FORMAT_PLAIN., |
| 37 | OUTPUT_FORMAT_CBMcxx . . $ OUTPUT_FORMAT_CBM., // default for "!to" pseudo opcode |
| 38 | }; |
| 39 | |
| 40 | |
| 41 | // Variables |
| 42 | |
| 43 | // segment stuff |
| 44 | static struct segment_tcxx . . $ segment_t#* segment_listcxx . . $ segment_list. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;// linked list |
| 45 | static intval_tcxx . . $ intval_t# segment_startcxx . . $ segment_start.;// Start of current segment |
| 46 | static intval_tcxx . . $ intval_t# segment_maxcxx . . $ segment_max.;// Highest address segment may use |
| 47 | // misc |
| 48 | static intval_tcxx . . $ intval_t# lowest_idxcxx . . $ lowest_idx.; // Smallest address program uses |
| 49 | static intval_tcxx . . $ intval_t# highest_idxcxx . . $ highest_idx.; // End address of program plus one |
| 50 | // output buffer stuff |
| 51 | static char* output_buffercxx . . $ output_buffer. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; // to hold assembled code |
| 52 | static char* write_ptrcxx . . $ write_ptr. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; // points into output_buffer |
| 53 | intval_tcxx . . $ intval_t# write_idxcxx . . $ write_idx.; // index in output buffer |
| 54 | static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! memory_initialisedcxx . . $ memory_initialised. = FALSEcxx . . $ `src/asm/config.h:54:9`!; |
| 55 | // chosen file format |
| 56 | static enum out_format_tcxx . . $ out_format_t# output_formatcxx . . $ output_format. = OUTPUT_FORMAT_CBMcxx . . $ OUTPUT_FORMAT_CBM.; |
| 57 | // predefined stuff |
| 58 | static node_tcxx . . $ node_t#* file_format_treecxx . . $ file_format_tree. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;// tree to hold output formats |
| 59 | // possible file formats |
| 60 | static node_tcxx . . $ node_t# file_formatscxx . . $ file_formats.[] = { |
| 61 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!(s_cbm, OUTPUT_FORMAT_CBM), |
| 62 | // PREDEFNODE("o65", OUTPUT_FORMAT_O65), |
| 63 | PREDEFLASTcxx . . $ `src/asm/tree.h:16:9`!("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_completelycxx . . $ fill_completely(44b6ea1973a07080).(char value) { |
| 72 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(output_buffercxx . . $ output_buffer., value, OUTBUFFERSIZEcxx . . $ `src/asm/output.c:33:9`!); |
| 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_maxcxx . . $ find_segment_max(1624ee8275b10bda).(intval_tcxx . . $ intval_t# new_pc) { |
| 78 | struct segment_tcxx . . $ segment_t#* test_segment; |
| 79 | |
| 80 | // may be faster if list is ordered! |
| 81 | segment_maxcxx . . $ segment_max. = OUTBUFFERSIZEcxx . . $ `src/asm/output.c:33:9`!;// will be decremented later! |
| 82 | test_segment = segment_listcxx . . $ segment_list.; |
| 83 | while(test_segment) { |
| 84 | if((test_segment->startcxx . . $ segment_t#start. > new_pc) |
| 85 | && (test_segment->startcxx . . $ segment_t#start. < segment_maxcxx . . $ segment_max.)) |
| 86 | segment_maxcxx . . $ segment_max. = test_segment->startcxx . . $ segment_t#start.; |
| 87 | test_segment = test_segment->nextcxx . . $ segment_t#next.; |
| 88 | } |
| 89 | segment_maxcxx . . $ segment_max.--;// last free address available |
| 90 | } |
| 91 | |
| 92 | // |
| 93 | static void border_crossedcxx . . $ border_crossed(d4f767463ce0a6b3).(int current_offset) { |
| 94 | if(current_offset >= OUTBUFFERSIZEcxx . . $ `src/asm/output.c:33:9`!) |
| 95 | Throw_serious_errorcxx . . $ Throw_serious_error(85c52e162fed56f9).("Produced too much code."); |
| 96 | if(pass_countcxx . . $ pass_count. == 0) { |
| 97 | Throw_warningcxx . . $ Throw_warning(85c52e162fed56f9).("Segment reached another one, overwriting it."); |
| 98 | find_segment_maxcxx . . $ find_segment_max(1624ee8275b10bda).(current_offset + 1); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // Send low byte to output buffer |
| 103 | void (*Output_bytecxx . . $ Output_byte.)(intval_tcxx . . $ intval_t# byte); |
| 104 | static void real_outputcxx . . $ real_output(1624ee8275b10bda).(intval_tcxx . . $ intval_t# byte); // fn for actual output |
| 105 | static void no_outputcxx . . $ no_output(1624ee8275b10bda).(intval_tcxx . . $ intval_t# byte); // fn when output impossible |
| 106 | |
| 107 | // set output pointer (negative values deactivate output) |
| 108 | static void set_mem_ptrcxx . . $ set_mem_ptr(1624ee8275b10bda).(signed long index) { |
| 109 | if(index < 0) { |
| 110 | Output_bytecxx . . $ Output_byte. = no_outputcxx . . $ no_output(1624ee8275b10bda).; |
| 111 | write_ptrcxx . . $ write_ptr. = output_buffercxx . . $ output_buffer.; |
| 112 | write_idxcxx . . $ write_idx. = 0; |
| 113 | } else { |
| 114 | Output_bytecxx . . $ Output_byte. = real_outputcxx . . $ real_output(1624ee8275b10bda).; |
| 115 | write_ptrcxx . . $ write_ptr. = output_buffercxx . . $ output_buffer. + index; |
| 116 | write_idxcxx . . $ write_idx. = index; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // Send low byte to output buffer, automatically increasing program counter |
| 121 | static void real_outputcxx . . $ real_output(1624ee8275b10bda).(intval_tcxx . . $ intval_t# byte) { |
| 122 | if(write_idxcxx . . $ write_idx. > segment_maxcxx . . $ segment_max.) |
| 123 | border_crossedcxx . . $ border_crossed(d4f767463ce0a6b3).(write_idxcxx . . $ write_idx.); |
| 124 | *write_ptrcxx . . $ write_ptr.++ = byte & 0xff; |
| 125 | write_idxcxx . . $ write_idx.++; |
| 126 | CPU_2addcxx . . $ CPU_2add.++; |
| 127 | } |
| 128 | |
| 129 | // fail to write to output buffer |
| 130 | static void no_outputcxx . . $ no_output(1624ee8275b10bda).(intval_tcxx . . $ intval_t# byte) { |
| 131 | Throw_errorcxx . . $ Throw_error(85c52e162fed56f9).(exception_pc_undefinedcxx . . $ 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_ptrcxx . . $ set_mem_ptr(1624ee8275b10bda).(512); // 512 to not garble zero page and stack. ;) |
| 135 | CPU_2addcxx . . $ CPU_2add.++; |
| 136 | } |
| 137 | |
| 138 | // call this if really calling Output_byte would be a waste of time |
| 139 | void Output_fakecxx . . $ Output_fake(d4f767463ce0a6b3).(int size) { |
| 140 | // check whether ptr undefined |
| 141 | if(Output_bytecxx . . $ Output_byte. == no_outputcxx . . $ no_output(1624ee8275b10bda).) { |
| 142 | no_outputcxx . . $ no_output(1624ee8275b10bda).(0); |
| 143 | size--; |
| 144 | } |
| 145 | if(write_idxcxx . . $ write_idx. + size - 1 > segment_maxcxx . . $ segment_max.) |
| 146 | border_crossedcxx . . $ border_crossed(d4f767463ce0a6b3).(write_idxcxx . . $ write_idx. + size - 1); |
| 147 | write_ptrcxx . . $ write_ptr. += size; |
| 148 | write_idxcxx . . $ write_idx. += size; |
| 149 | CPU_2addcxx . . $ CPU_2add. += size; |
| 150 | } |
| 151 | |
| 152 | // Output 8-bit value with range check |
| 153 | void Output_8bcxx . . $ Output_8b(1624ee8275b10bda).(intval_tcxx . . $ intval_t# value) { |
| 154 | if((value <= 0xff) && (value >= -0x80)) |
| 155 | Output_bytecxx . . $ Output_byte.(value); |
| 156 | else |
| 157 | Throw_errorcxx . . $ Throw_error(85c52e162fed56f9).(exception_number_out_of_rangecxx . . $ exception_number_out_of_range.); |
| 158 | } |
| 159 | |
| 160 | // Output 16-bit value with range check |
| 161 | void Output_16bcxx . . $ Output_16b(1624ee8275b10bda).(intval_tcxx . . $ intval_t# value) { |
| 162 | if((value <= 0xffff) && (value >= -0x8000)) { |
| 163 | Output_bytecxx . . $ Output_byte.(value); |
| 164 | Output_bytecxx . . $ Output_byte.(value >> 8); |
| 165 | } else |
| 166 | Throw_errorcxx . . $ Throw_error(85c52e162fed56f9).(exception_number_out_of_rangecxx . . $ exception_number_out_of_range.); |
| 167 | } |
| 168 | |
| 169 | // Output 24-bit value with range check |
| 170 | void Output_24bcxx . . $ Output_24b(1624ee8275b10bda).(intval_tcxx . . $ intval_t# value) { |
| 171 | if((value <= 0xffffff) && (value >= -0x800000)) { |
| 172 | Output_bytecxx . . $ Output_byte.(value); |
| 173 | Output_bytecxx . . $ Output_byte.(value >> 8); |
| 174 | Output_bytecxx . . $ Output_byte.(value >> 16); |
| 175 | } else |
| 176 | Throw_errorcxx . . $ Throw_error(85c52e162fed56f9).(exception_number_out_of_rangecxx . . $ exception_number_out_of_range.); |
| 177 | } |
| 178 | |
| 179 | // Output 32-bit value (without range check) |
| 180 | void Output_32bcxx . . $ Output_32b(1624ee8275b10bda).(intval_tcxx . . $ intval_t# value) { |
| 181 | // if((Value <= 0x7fffffff) && (Value >= -0x80000000)) { |
| 182 | Output_bytecxx . . $ Output_byte.(value); |
| 183 | Output_bytecxx . . $ Output_byte.(value >> 8); |
| 184 | Output_bytecxx . . $ Output_byte.(value >> 16); |
| 185 | Output_bytecxx . . $ 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_tcxx . . $ eos_t# PO_initmemcxx . . $ PO_initmem(6522f478c9c0d97c).(void) { |
| 192 | intval_tcxx . . $ intval_t# content; |
| 193 | |
| 194 | // ignore in all passes but in first |
| 195 | if(pass_countcxx . . $ pass_count.) |
| 196 | return(SKIP_REMAINDERcxx . . $ SKIP_REMAINDER.); |
| 197 | // if MemInit flag is already set, complain |
| 198 | if(memory_initialisedcxx . . $ memory_initialised.) { |
| 199 | Throw_warningcxx . . $ Throw_warning(85c52e162fed56f9).("Memory already initialised."); |
| 200 | return(SKIP_REMAINDERcxx . . $ SKIP_REMAINDER.); |
| 201 | } |
| 202 | // set MemInit flag |
| 203 | memory_initialisedcxx . . $ memory_initialised. = TRUEcxx . . $ `src/asm/config.h:55:9`!; |
| 204 | // get value and init memory |
| 205 | content = ALU_defined_intcxx . . $ ALU_defined_int(da6335ded91d79c2).(); |
| 206 | if((content > 0xff) || (content < -0x80)) |
| 207 | Throw_errorcxx . . $ Throw_error(85c52e162fed56f9).(exception_number_out_of_rangecxx . . $ exception_number_out_of_range.); |
| 208 | // init memory |
| 209 | fill_completelycxx . . $ fill_completely(44b6ea1973a07080).(content); |
| 210 | // enforce another pass |
| 211 | if(pass_undefined_countcxx . . $ pass_undefined_count. == 0) |
| 212 | pass_undefined_countcxx . . $ 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_EOScxx . . $ ENSURE_EOS.); |
| 217 | } |
| 218 | |
| 219 | // Try to set output format held in DynaBuf. Returns whether succeeded. |
| 220 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! Output_set_output_formatcxx . . $ Output_set_output_format(c9857a73d68e59ba).(void) { |
| 221 | void* node_body; |
| 222 | |
| 223 | if(!Tree_easy_scancxx . . $ Tree_easy_scan(7f10be3e8224db50).(file_format_treecxx . . $ file_format_tree., &node_body, GlobalDynaBufcxx . . $ GlobalDynaBuf.)) |
| 224 | return(FALSEcxx . . $ `src/asm/config.h:54:9`!); |
| 225 | output_formatcxx . . $ output_format. = (enum out_format_tcxx . . $ out_format_t#) node_body; |
| 226 | return(TRUEcxx . . $ `src/asm/config.h:55:9`!); |
| 227 | } |
| 228 | |
| 229 | // Select output file and format ("!to" pseudo opcode) |
| 230 | static enum eos_tcxx . . $ eos_t# PO_tocxx . . $ PO_to(6522f478c9c0d97c).(void) { |
| 231 | // only act upon this pseudo opcode in first pass |
| 232 | if(pass_countcxx . . $ pass_count.) |
| 233 | return(SKIP_REMAINDERcxx . . $ SKIP_REMAINDER.); |
| 234 | // if output file already chosen, complain and exit |
| 235 | if(output_filenamecxx . . $ output_filename.) { |
| 236 | Throw_warningcxx . . $ Throw_warning(85c52e162fed56f9).("Output file already chosen."); |
| 237 | return(SKIP_REMAINDERcxx . . $ 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_filenamecxx . . $ Input_read_filename(efaadfb7dc14930a).(FALSEcxx . . $ `src/asm/config.h:54:9`!)) |
| 242 | return(SKIP_REMAINDERcxx . . $ SKIP_REMAINDER.); |
| 243 | // get malloc'd copy of filename |
| 244 | output_filenamecxx . . $ output_filename. = DynaBuf_get_copycxx . . $ DynaBuf_get_copy(d7229e135d075701).(GlobalDynaBufcxx . . $ GlobalDynaBuf.); |
| 245 | // select output format |
| 246 | // if no comma found, use default file format |
| 247 | if(Input_accept_commacxx . . $ Input_accept_comma(c9857a73d68e59ba).() == FALSEcxx . . $ `src/asm/config.h:54:9`!) { |
| 248 | if(output_formatcxx . . $ output_format. == OUTPUT_FORMAT_UNSPECIFIEDcxx . . $ OUTPUT_FORMAT_UNSPECIFIED.) { |
| 249 | output_formatcxx . . $ output_format. = OUTPUT_FORMAT_CBMcxx . . $ OUTPUT_FORMAT_CBM.; |
| 250 | // output deprecation warning |
| 251 | Throw_warningcxx . . $ Throw_warning(85c52e162fed56f9).("Used \"!to\" without file format indicator. Defaulting to \"cbm\"."); |
| 252 | } |
| 253 | return(ENSURE_EOScxx . . $ ENSURE_EOS.); |
| 254 | } |
| 255 | // parse output format name |
| 256 | // if no keyword given, give up |
| 257 | if(Input_read_and_lower_keywordcxx . . $ Input_read_and_lower_keyword(b126dc7c1de90089).() == 0) |
| 258 | return(SKIP_REMAINDERcxx . . $ SKIP_REMAINDER.); |
| 259 | if(Output_set_output_formatcxx . . $ Output_set_output_format(c9857a73d68e59ba).()) |
| 260 | return(ENSURE_EOScxx . . $ ENSURE_EOS.); // success |
| 261 | // error occurred |
| 262 | Throw_errorcxx . . $ Throw_error(85c52e162fed56f9).("Unknown output format."); |
| 263 | return(SKIP_REMAINDERcxx . . $ SKIP_REMAINDER.); |
| 264 | } |
| 265 | |
| 266 | // pseudo ocpode table |
| 267 | static node_tcxx . . $ node_t# pseudo_opcodescxx . . $ pseudo_opcodes.[] = { |
| 268 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("initmem", PO_initmem), |
| 269 | PREDEFLASTcxx . . $ `src/asm/tree.h:16:9`!("to", PO_to), |
| 270 | // ^^^^ this marks the last element |
| 271 | }; |
| 272 | |
| 273 | // Init file format tree (is done early) |
| 274 | void Outputfile_initcxx . . $ Outputfile_init(49f6e7a06ebc5aa8).(void) { |
| 275 | Tree_add_tablecxx . . $ Tree_add_table(90ba9e887489f1c).(&file_format_treecxx . . $ file_format_tree., file_formatscxx . . $ file_formats.); |
| 276 | } |
| 277 | |
| 278 | // alloc and init mem buffer, register pseudo opcodes (done later) |
| 279 | void Output_initcxx . . $ Output_init(1624ee8275b10bda).(signed long fill_value) { |
| 280 | output_buffercxx . . $ output_buffer. = safe_malloccxx . . $ safe_malloc(bfcfa4d6b7f7ef64).(OUTBUFFERSIZEcxx . . $ `src/asm/output.c:33:9`!); |
| 281 | write_ptrcxx . . $ write_ptr. = output_buffercxx . . $ output_buffer.; |
| 282 | if(fill_value == MEMINIT_USE_DEFAULTcxx . . $ `src/asm/output.h:15:9`!) |
| 283 | fill_value = FILLVALUE_INITIALcxx . . $ `src/asm/config.h:44:9`!; |
| 284 | else |
| 285 | memory_initialisedcxx . . $ memory_initialised. = TRUEcxx . . $ `src/asm/config.h:55:9`!; |
| 286 | // init output buffer (fill memory with initial value) |
| 287 | fill_completelycxx . . $ fill_completely(44b6ea1973a07080).(fill_value & 0xff); |
| 288 | Tree_add_tablecxx . . $ Tree_add_table(90ba9e887489f1c).(&pseudo_opcode_treecxx . . $ pseudo_opcode_tree., pseudo_opcodescxx . . $ pseudo_opcodes.); |
| 289 | } |
| 290 | |
| 291 | // Dump memory buffer into output file |
| 292 | void Output_save_filecxx . . $ Output_save_file(8d4262d45fc644ab).(FILEcxx . . $ FILE#* fd) { |
| 293 | intval_tcxx . . $ intval_t# amount = highest_idxcxx . . $ highest_idx. - lowest_idxcxx . . $ lowest_idx.; |
| 294 | |
| 295 | if(Process_verbositycxx . . $ Process_verbosity.) |
| 296 | printfcxx . . $ printf(581fddd72a8db8fc).("Saving %ld ($%lx) bytes ($%lx - $%lx exclusive).\n", |
| 297 | amount, amount, lowest_idxcxx . . $ lowest_idx., highest_idxcxx . . $ highest_idx.); |
| 298 | // Output file header according to file format |
| 299 | switch(output_formatcxx . . $ output_format.) { |
| 300 | |
| 301 | case OUTPUT_FORMAT_UNSPECIFIEDcxx . . $ OUTPUT_FORMAT_UNSPECIFIED.: |
| 302 | case OUTPUT_FORMAT_PLAINcxx . . $ OUTPUT_FORMAT_PLAIN.: |
| 303 | PLATFORM_SETFILETYPE_PLAINcxx . . $ `src/asm/_std.h:24:9`!(output_filename); |
| 304 | break; |
| 305 | |
| 306 | case OUTPUT_FORMAT_CBMcxx . . $ OUTPUT_FORMAT_CBM.: |
| 307 | PLATFORM_SETFILETYPE_CBMcxx . . $ `src/asm/_std.h:23:9`!(output_filename); |
| 308 | // output 16-bit load address in little-endian byte order |
| 309 | putccxx . . $ putc(b1cd03d6c470d1bd).(lowest_idxcxx . . $ lowest_idx. & 255, fd); |
| 310 | putccxx . . $ putc(b1cd03d6c470d1bd).(lowest_idxcxx . . $ lowest_idx. >> 8, fd); |
| 311 | break; |
| 312 | |
| 313 | } |
| 314 | // Dump output buffer to file |
| 315 | fwritecxx . . $ fwrite(ab0609fa07c47d6e).(output_buffercxx . . $ output_buffer. + lowest_idxcxx . . $ lowest_idx., sizeof(char), amount, fd); |
| 316 | } |
| 317 | |
| 318 | char* Output_get_final_datacxx . . $ Output_get_final_data(3f31f7b3b80edb32).(int *length) { |
| 319 | intval_tcxx . . $ intval_t# amount = highest_idxcxx . . $ highest_idx. - lowest_idxcxx . . $ lowest_idx.; |
| 320 | int bufsize = amount * sizeof(char); |
| 321 | if(Process_verbositycxx . . $ Process_verbosity.) |
| 322 | printfcxx . . $ printf(581fddd72a8db8fc).("Saving %ld ($%lx) bytes ($%lx - $%lx exclusive).\n", |
| 323 | amount, amount, lowest_idxcxx . . $ lowest_idx., highest_idxcxx . . $ highest_idx.); |
| 324 | // Output file header according to file format |
| 325 | switch(output_formatcxx . . $ output_format.) { |
| 326 | |
| 327 | case OUTPUT_FORMAT_UNSPECIFIEDcxx . . $ OUTPUT_FORMAT_UNSPECIFIED.: |
| 328 | case OUTPUT_FORMAT_PLAINcxx . . $ OUTPUT_FORMAT_PLAIN.: |
| 329 | PLATFORM_SETFILETYPE_PLAINcxx . . $ `src/asm/_std.h:24:9`!(output_filename); |
| 330 | break; |
| 331 | |
| 332 | case OUTPUT_FORMAT_CBMcxx . . $ OUTPUT_FORMAT_CBM.: |
| 333 | PLATFORM_SETFILETYPE_CBMcxx . . $ `src/asm/_std.h:23:9`!(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 = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(bufsize); |
| 348 | if(output_formatcxx . . $ output_format. == OUTPUT_FORMAT_CBMcxx . . $ OUTPUT_FORMAT_CBM.) { |
| 349 | buffer[0] = lowest_idxcxx . . $ lowest_idx. & 255; |
| 350 | buffer[1] = lowest_idxcxx . . $ lowest_idx. >> 8; |
| 351 | memcpycxx . . $ memcpy(cc817cb175f282d).(buffer + 2, output_buffercxx . . $ output_buffer. + lowest_idxcxx . . $ lowest_idx., amount); |
| 352 | } |
| 353 | else { |
| 354 | memcpycxx . . $ memcpy(cc817cb175f282d).(buffer, output_buffercxx . . $ output_buffer. + lowest_idxcxx . . $ lowest_idx., amount); |
| 355 | } |
| 356 | *length = bufsize; |
| 357 | return buffer; |
| 358 | } |
| 359 | |
| 360 | |
| 361 | // Link segment data into segment chain |
| 362 | static void link_segmentcxx . . $ link_segment(a8896fd0f69df779).(intval_tcxx . . $ intval_t# start, intval_tcxx . . $ intval_t# length) { |
| 363 | struct segment_tcxx . . $ segment_t#* new_segment; |
| 364 | |
| 365 | new_segment = safe_malloccxx . . $ safe_malloc(bfcfa4d6b7f7ef64).(sizeof(struct segment_tcxx . . $ segment_t#)); |
| 366 | new_segment->startcxx . . $ segment_t#start. = start; |
| 367 | new_segment->lengthcxx . . $ segment_t#length. = length; |
| 368 | new_segment->nextcxx . . $ segment_t#next. = segment_listcxx . . $ segment_list.; |
| 369 | segment_listcxx . . $ 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_segmentcxx . . $ Output_end_segment(49f6e7a06ebc5aa8).(void) { |
| 375 | intval_tcxx . . $ intval_t# amount; |
| 376 | |
| 377 | if(CPU_uses_pseudo_pccxx . . $ CPU_uses_pseudo_pc(c9857a73d68e59ba).()) |
| 378 | Throw_first_pass_warningcxx . . $ Throw_first_pass_warning(85c52e162fed56f9).("Offset assembly still active at end of segment."); // FIXME - should be error! |
| 379 | if(pass_countcxx . . $ pass_count. == 0) { |
| 380 | amount = write_idxcxx . . $ write_idx. - segment_startcxx . . $ segment_start.; |
| 381 | link_segmentcxx . . $ link_segment(a8896fd0f69df779).(segment_startcxx . . $ segment_start., amount); |
| 382 | if(Process_verbositycxx . . $ Process_verbosity. > 1) |
| 383 | printfcxx . . $ printf(581fddd72a8db8fc).( |
| 384 | "Segment size is %ld ($%lx) bytes ($%lx - $%lx exclusive).\n", |
| 385 | amount, amount, segment_startcxx . . $ segment_start., write_idxcxx . . $ 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_idxcxx . . $ write_idx. > highest_idxcxx . . $ highest_idx.) |
| 391 | highest_idxcxx . . $ highest_idx. = write_idxcxx . . $ write_idx.; |
| 392 | } |
| 393 | |
| 394 | // Check whether given PC is inside segment. |
| 395 | static void check_segmentcxx . . $ check_segment(1624ee8275b10bda).(intval_tcxx . . $ intval_t# new_pc) { |
| 396 | struct segment_tcxx . . $ segment_t#* test_segment; |
| 397 | |
| 398 | test_segment = segment_listcxx . . $ segment_list.; |
| 399 | while(test_segment) { |
| 400 | if((new_pc >= test_segment->startcxx . . $ segment_t#start.) |
| 401 | && (new_pc < (test_segment->startcxx . . $ segment_t#start.) + (test_segment->lengthcxx . . $ segment_t#length.))) |
| 402 | Throw_warningcxx . . $ Throw_warning(85c52e162fed56f9).("Segment starts inside another one, overwriting it."); |
| 403 | test_segment = test_segment->nextcxx . . $ segment_t#next.; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | // init lowest and highest address |
| 408 | static void init_borderscxx . . $ init_borders(1624ee8275b10bda).(intval_tcxx . . $ intval_t# address) { |
| 409 | lowest_idxcxx . . $ lowest_idx. = address; |
| 410 | highest_idxcxx . . $ highest_idx. = address; |
| 411 | } |
| 412 | |
| 413 | // clear segment list |
| 414 | void Output_passinitcxx . . $ Output_passinit(1624ee8275b10bda).(signed long start_addr) { |
| 415 | struct segment_tcxx . . $ segment_t#* temp; |
| 416 | |
| 417 | // delete segment list (and free blocks) |
| 418 | while((temp = segment_listcxx . . $ segment_list.)) { |
| 419 | segment_listcxx . . $ segment_list. = segment_listcxx . . $ segment_list.->nextcxx . . $ segment_t#next.; |
| 420 | freecxx . . $ free(5d22bdacc48458e8).(temp); |
| 421 | } |
| 422 | set_mem_ptrcxx . . $ set_mem_ptr(1624ee8275b10bda).(start_addr); // negative values deactivate output |
| 423 | // if start address given, set program counter |
| 424 | if(start_addr >= 0) { |
| 425 | CPU_set_pccxx . . $ CPU_set_pc(1624ee8275b10bda).(start_addr); |
| 426 | // FIXME - integrate next two? |
| 427 | init_borderscxx . . $ init_borders(1624ee8275b10bda).(start_addr); |
| 428 | segment_startcxx . . $ segment_start. = start_addr; |
| 429 | } else { |
| 430 | init_borderscxx . . $ init_borders(1624ee8275b10bda).(0); // set to _something_ (for !initmem) |
| 431 | segment_startcxx . . $ segment_start. = 0; |
| 432 | } |
| 433 | // other stuff |
| 434 | segment_maxcxx . . $ segment_max. = OUTBUFFERSIZEcxx . . $ `src/asm/output.c:33:9`!-1; |
| 435 | } |
| 436 | |
| 437 | // Called when "*=EXPRESSION" is parsed |
| 438 | void Output_start_segmentcxx . . $ Output_start_segment(49f6e7a06ebc5aa8).(void) { |
| 439 | intval_tcxx . . $ intval_t# new_addr = ALU_defined_intcxx . . $ ALU_defined_int(da6335ded91d79c2).(); |
| 440 | |
| 441 | if(CPU_pccxx . . $ CPU_pc..flagscxx . . $ result_int_t#flags. & MVALUE_DEFINEDcxx . . $ `src/asm/alu.h:27:9`!) { |
| 442 | // It's a redefinition. Check some things: |
| 443 | // check whether new low |
| 444 | if(new_addr < lowest_idxcxx . . $ lowest_idx.) |
| 445 | lowest_idxcxx . . $ lowest_idx. = new_addr; |
| 446 | // show status of previous segment |
| 447 | Output_end_segmentcxx . . $ Output_end_segment(49f6e7a06ebc5aa8).(); |
| 448 | // in first pass, maybe issue warning |
| 449 | if(pass_countcxx . . $ pass_count. == 0) { |
| 450 | check_segmentcxx . . $ check_segment(1624ee8275b10bda).(new_addr); |
| 451 | find_segment_maxcxx . . $ find_segment_max(1624ee8275b10bda).(new_addr); |
| 452 | } |
| 453 | } else |
| 454 | init_borderscxx . . $ init_borders(1624ee8275b10bda).(new_addr);// it's the first pc definition |
| 455 | CPU_set_pccxx . . $ CPU_set_pc(1624ee8275b10bda).(new_addr); |
| 456 | segment_startcxx . . $ segment_start. = new_addr; |
| 457 | set_mem_ptrcxx . . $ set_mem_ptr(1624ee8275b10bda).(new_addr); |
| 458 | } |