| 1 | //cxx . . $ `<file>/src/asm/basics.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 | // basic assembly stuff |
| 7 | |
| 8 | #include <stdlib.h> |
| 9 | #include <stdio.h> |
| 10 | #include "config.h"cxx . . $ `<file>/src/asm/config.h`/ |
| 11 | #include "cpu.h"cxx . . $ `<file>/src/asm/cpu.h`/ |
| 12 | #include "basics.h"cxx . . $ `<file>/src/asm/basics.h`/ |
| 13 | #include "alu.h"cxx . . $ `<file>/src/asm/alu.h`/ |
| 14 | #include "dynabuf.h"cxx . . $ `<file>/src/asm/dynabuf.h`/ |
| 15 | #include "input.h"cxx . . $ `<file>/src/asm/input.h`/ |
| 16 | #include "global.h"cxx . . $ `<file>/src/asm/global.h`/ |
| 17 | #include "output.h"cxx . . $ `<file>/src/asm/output.h`/ |
| 18 | #include "tree.h"cxx . . $ `<file>/src/asm/tree.h`/ |
| 19 | |
| 20 | |
| 21 | // Constants |
| 22 | #define USERMSG_DYNABUF_INITIALSIZEcxx . . $ `src/asm/basics.c:22:9`! 80 |
| 23 | static const char s_08cxx . . $ s_08.[] = "08"; |
| 24 | #define s_8cxx . . $ `src/asm/basics.c:24:9`! (s_08+1) // Yes, I know I'm sick |
| 25 | #define s_16cxx . . $ `src/asm/basics.c:25:9`! (s_65816+3) // Yes, I know I'm sick |
| 26 | |
| 27 | |
| 28 | // Variables |
| 29 | static dynabuf_tcxx . . $ dynabuf_t#* user_messagecxx . . $ user_message.; // dynamic buffer (!warn/error/serious) |
| 30 | |
| 31 | |
| 32 | // Functions |
| 33 | |
| 34 | // Helper function for !8, !16, !24 and !32 pseudo opcodes |
| 35 | static enum eos_tcxx . . $ eos_t# output_objectscxx . . $ output_objects(98e82df25d6c1911).(void (*fn)(intval_tcxx . . $ intval_t#)) { |
| 36 | do |
| 37 | fn(ALU_any_intcxx . . $ ALU_any_int(da6335ded91d79c2).()); |
| 38 | while(Input_accept_commacxx . . $ Input_accept_comma(c9857a73d68e59ba).()); |
| 39 | return(ENSURE_EOScxx . . $ ENSURE_EOS.); |
| 40 | } |
| 41 | |
| 42 | // Insert 8-bit values ("!08" / "!8" / "!by" / "!byte" pseudo opcode) |
| 43 | static enum eos_tcxx . . $ eos_t# PO_08cxx . . $ PO_08(6522f478c9c0d97c).(void) { |
| 44 | return(output_objectscxx . . $ output_objects(98e82df25d6c1911).(Output_8bcxx . . $ Output_8b(1624ee8275b10bda).)); |
| 45 | } |
| 46 | |
| 47 | // Insert 16-bit values ("!16" / "!wo" / "!word" pseudo opcode) |
| 48 | static enum eos_tcxx . . $ eos_t# PO_16cxx . . $ PO_16(6522f478c9c0d97c).(void) { |
| 49 | return(output_objectscxx . . $ output_objects(98e82df25d6c1911).(Output_16bcxx . . $ Output_16b(1624ee8275b10bda).)); |
| 50 | } |
| 51 | |
| 52 | // Insert 24-bit values ("!24" pseudo opcode) |
| 53 | static enum eos_tcxx . . $ eos_t# PO_24cxx . . $ PO_24(6522f478c9c0d97c).(void) { |
| 54 | return(output_objectscxx . . $ output_objects(98e82df25d6c1911).(Output_24bcxx . . $ Output_24b(1624ee8275b10bda).)); |
| 55 | } |
| 56 | |
| 57 | // Insert 32-bit values ("!32" pseudo opcode) |
| 58 | static enum eos_tcxx . . $ eos_t# PO_32cxx . . $ PO_32(6522f478c9c0d97c).(void) { |
| 59 | return(output_objectscxx . . $ output_objects(98e82df25d6c1911).(Output_32bcxx . . $ Output_32b(1624ee8275b10bda).)); |
| 60 | } |
| 61 | |
| 62 | // Include binary file |
| 63 | static enum eos_tcxx . . $ eos_t# PO_binarycxx . . $ PO_binary(6522f478c9c0d97c).(void) { |
| 64 | FILEcxx . . $ FILE#* fd; |
| 65 | int byte; |
| 66 | intval_tcxx . . $ intval_t# size = -1, // means "not given" => "until EOF" |
| 67 | skip = 0; |
| 68 | |
| 69 | // if file name is missing, don't bother continuing |
| 70 | if(Input_read_filenamecxx . . $ Input_read_filename(efaadfb7dc14930a).(TRUEcxx . . $ `src/asm/config.h:55:9`!)) |
| 71 | return(SKIP_REMAINDERcxx . . $ SKIP_REMAINDER.); |
| 72 | // try to open file |
| 73 | fd = fopencxx . . $ fopen(82ec0787cd37e0a3).(GLOBALDYNABUF_CURRENTcxx . . $ `src/asm/dynabuf.h:23:9`!, FILE_READBINARYcxx . . $ FILE_READBINARY.); |
| 74 | if(fd == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 75 | Throw_errorcxx . . $ Throw_error(85c52e162fed56f9).(exception_cannot_open_input_filecxx . . $ exception_cannot_open_input_file.); |
| 76 | return(SKIP_REMAINDERcxx . . $ SKIP_REMAINDER.); |
| 77 | } |
| 78 | // read optional arguments |
| 79 | if(Input_accept_commacxx . . $ Input_accept_comma(c9857a73d68e59ba).()) { |
| 80 | if(ALU_optional_defined_intcxx . . $ ALU_optional_defined_int(c894b17f3c089378).(&size) |
| 81 | && (size <0)) |
| 82 | Throw_serious_errorcxx . . $ Throw_serious_error(85c52e162fed56f9).("Negative size argument."); |
| 83 | if(Input_accept_commacxx . . $ Input_accept_comma(c9857a73d68e59ba).()) |
| 84 | ALU_optional_defined_intcxx . . $ ALU_optional_defined_int(c894b17f3c089378).(&skip);// read skip |
| 85 | } |
| 86 | // check whether including is a waste of time |
| 87 | if((size >= 0) && (pass_undefined_countcxx . . $ pass_undefined_count. || pass_real_errorscxx . . $ pass_real_errors.)) |
| 88 | Output_fakecxx . . $ Output_fake(d4f767463ce0a6b3).(size); // really including is useless anyway |
| 89 | else { |
| 90 | // really insert file |
| 91 | fseekcxx . . $ fseek(543fae30d249f6b1).(fd, skip, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:114:9`!); // set read pointer |
| 92 | // if "size" non-negative, read "size" bytes. |
| 93 | // otherwise, read until EOF. |
| 94 | while(size != 0) { |
| 95 | byte = getccxx . . $ getc(89bc1efcdac0ca22).(fd); |
| 96 | if(byte == EOFcxx . . $ `<external>/232824719582829767/stdio.h:109:9`!) |
| 97 | break; |
| 98 | Output_bytecxx . . $ Output_byte.(byte); |
| 99 | size--; |
| 100 | } |
| 101 | // if more should have been read, warn and add padding |
| 102 | if(size > 0) { |
| 103 | Throw_warningcxx . . $ Throw_warning(85c52e162fed56f9).("Padding with zeroes."); |
| 104 | do |
| 105 | Output_bytecxx . . $ Output_byte.(0); |
| 106 | while(--size); |
| 107 | } |
| 108 | } |
| 109 | fclosecxx . . $ fclose(89bc1efcdac0ca22).(fd); |
| 110 | // if verbose, produce some output |
| 111 | if((pass_countcxx . . $ pass_count. == 0) && (Process_verbositycxx . . $ Process_verbosity. > 1)) |
| 112 | printfcxx . . $ printf(581fddd72a8db8fc).("Loaded %d ($%x) bytes from file offset %ld ($%lx).\n", |
| 113 | CPU_2addcxx . . $ CPU_2add., CPU_2addcxx . . $ CPU_2add., skip, skip); |
| 114 | return(ENSURE_EOScxx . . $ ENSURE_EOS.); |
| 115 | } |
| 116 | |
| 117 | // Reserve space by sending bytes of given value ("!fi" / "!fill" pseudo opcode) |
| 118 | static enum eos_tcxx . . $ eos_t# PO_fillcxx . . $ PO_fill(6522f478c9c0d97c).(void) { |
| 119 | intval_tcxx . . $ intval_t# fill = FILLVALUE_FILLcxx . . $ `src/asm/config.h:46:9`!, |
| 120 | size = ALU_defined_intcxx . . $ ALU_defined_int(da6335ded91d79c2).(); |
| 121 | |
| 122 | if(Input_accept_commacxx . . $ Input_accept_comma(c9857a73d68e59ba).()) |
| 123 | fill = ALU_any_intcxx . . $ ALU_any_int(da6335ded91d79c2).(); |
| 124 | while(size--) |
| 125 | Output_8bcxx . . $ Output_8b(1624ee8275b10bda).(fill); |
| 126 | return(ENSURE_EOScxx . . $ ENSURE_EOS.); |
| 127 | } |
| 128 | |
| 129 | // show user-defined message |
| 130 | static enum eos_tcxx . . $ eos_t# throw_stringcxx . . $ throw_string(6f25eb228b2b24fe).(const char prefix[], void (*fn)(const char*)) { |
| 131 | result_tcxx . . $ result_t# result; |
| 132 | |
| 133 | DYNABUF_CLEARcxx . . $ `src/asm/dynabuf.h:14:9`!(user_message); |
| 134 | DynaBuf_add_stringcxx . . $ DynaBuf_add_string(9294670b9d26a99f).(user_messagecxx . . $ user_message., prefix); |
| 135 | do { |
| 136 | if(GotBytecxx . . $ GotByte. == '"') { |
| 137 | // parse string |
| 138 | GetQuotedBytecxx . . $ GetQuotedByte(6e9c618e4dd2eafc).(); // read initial character |
| 139 | // send characters until closing quote is reached |
| 140 | while(GotBytecxx . . $ GotByte. && (GotBytecxx . . $ GotByte. != '"')) { |
| 141 | DYNABUF_APPENDcxx . . $ `src/asm/dynabuf.h:15:9`!(user_message, GotByte); |
| 142 | GetQuotedBytecxx . . $ GetQuotedByte(6e9c618e4dd2eafc).(); |
| 143 | } |
| 144 | if(GotBytecxx . . $ GotByte. == CHAR_EOScxx . . $ `src/asm/input.h:48:9`!) |
| 145 | return(AT_EOS_ANYWAYcxx . . $ AT_EOS_ANYWAY.); |
| 146 | // after closing quote, proceed with next char |
| 147 | GetBytecxx . . $ GetByte(6e9c618e4dd2eafc).(); |
| 148 | } else { |
| 149 | // parse value |
| 150 | ALU_any_resultcxx . . $ ALU_any_result(64a63ef514ec9b8a).(&result); |
| 151 | if(result.flagscxx . . $ result_t#flags. & MVALUE_IS_FPcxx . . $ `src/asm/alu.h:16:9`!) { |
| 152 | // floating point |
| 153 | if(result.flagscxx . . $ result_t#flags. & MVALUE_DEFINEDcxx . . $ `src/asm/alu.h:27:9`!) |
| 154 | DynaBuf_add_doublecxx . . $ DynaBuf_add_double(fc902d48bef49e75).( |
| 155 | user_messagecxx . . $ user_message., |
| 156 | result.valcxx . . $ result_t#val..fpvalcxx . . $ result_t#$anonymous_type_0#fpval.); |
| 157 | else |
| 158 | DynaBuf_add_stringcxx . . $ DynaBuf_add_string(9294670b9d26a99f).( |
| 159 | user_messagecxx . . $ user_message., |
| 160 | "<UNDEFINED FLOAT>"); |
| 161 | } else { |
| 162 | // integer |
| 163 | if(result.flagscxx . . $ result_t#flags. & MVALUE_DEFINEDcxx . . $ `src/asm/alu.h:27:9`!) |
| 164 | DynaBuf_add_signed_longcxx . . $ DynaBuf_add_signed_long(643f1e5af5fbac8).( |
| 165 | user_messagecxx . . $ user_message., |
| 166 | result.valcxx . . $ result_t#val..intvalcxx . . $ result_t#$anonymous_type_0#intval.); |
| 167 | else |
| 168 | DynaBuf_add_stringcxx . . $ DynaBuf_add_string(9294670b9d26a99f).( |
| 169 | user_messagecxx . . $ user_message., |
| 170 | "<UNDEFINED INT>"); |
| 171 | } |
| 172 | } |
| 173 | } while(Input_accept_commacxx . . $ Input_accept_comma(c9857a73d68e59ba).()); |
| 174 | DynaBuf_appendcxx . . $ DynaBuf_append(11d1e1bb7092144).(user_messagecxx . . $ user_message., '\0'); |
| 175 | fn(user_messagecxx . . $ user_message.->buffercxx . . $ dynabuf_t#buffer.); |
| 176 | return(ENSURE_EOScxx . . $ ENSURE_EOS.); |
| 177 | } |
| 178 | |
| 179 | //// |
| 180 | //static enum eos_t PO_print(void) { |
| 181 | // return(throw_string()); |
| 182 | //} |
| 183 | |
| 184 | // throw warning as given in source code |
| 185 | static enum eos_tcxx . . $ eos_t# PO_warncxx . . $ PO_warn(6522f478c9c0d97c).(void) { |
| 186 | return(throw_stringcxx . . $ throw_string(6f25eb228b2b24fe).("!warn: ", Throw_warningcxx . . $ Throw_warning(85c52e162fed56f9).)); |
| 187 | } |
| 188 | |
| 189 | // throw error as given in source code |
| 190 | static enum eos_tcxx . . $ eos_t# PO_errorcxx . . $ PO_error(6522f478c9c0d97c).(void) { |
| 191 | return(throw_stringcxx . . $ throw_string(6f25eb228b2b24fe).("!error: ", Throw_errorcxx . . $ Throw_error(85c52e162fed56f9).)); |
| 192 | } |
| 193 | |
| 194 | // throw serious error as given in source code |
| 195 | static enum eos_tcxx . . $ eos_t# PO_seriouscxx . . $ PO_serious(6522f478c9c0d97c).(void) { |
| 196 | return(throw_stringcxx . . $ throw_string(6f25eb228b2b24fe).("!serious: ", Throw_serious_errorcxx . . $ Throw_serious_error(85c52e162fed56f9).)); |
| 197 | } |
| 198 | |
| 199 | // pseudo ocpode table |
| 200 | static node_tcxx . . $ node_t# pseudo_opcodescxx . . $ pseudo_opcodes.[] = { |
| 201 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!(s_08, PO_08), |
| 202 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!(s_8cxx . . $ `src/asm/basics.c:24:9`!, PO_08), |
| 203 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("by", PO_08), |
| 204 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("byte", PO_08), |
| 205 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!(s_16cxx . . $ `src/asm/basics.c:25:9`!, PO_16), |
| 206 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("wo", PO_16), |
| 207 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("word", PO_16), |
| 208 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("24", PO_24), |
| 209 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("32", PO_32), |
| 210 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("bin", PO_binary), |
| 211 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("binary", PO_binary), |
| 212 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("fi", PO_fill), |
| 213 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("fill", PO_fill), |
| 214 | // PREDEFNODE("print", PO_print), |
| 215 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("warn", PO_warn), |
| 216 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!(s_error, PO_error), |
| 217 | PREDEFLASTcxx . . $ `src/asm/tree.h:16:9`!("serious", PO_serious), |
| 218 | // ^^^^ this marks the last element |
| 219 | }; |
| 220 | |
| 221 | // register pseudo opcodes and create dynamic buffer |
| 222 | void Basics_initcxx . . $ Basics_init(49f6e7a06ebc5aa8).(void) { |
| 223 | user_messagecxx . . $ user_message. = DynaBuf_createcxx . . $ DynaBuf_create(139370366a5bf8f7).(USERMSG_DYNABUF_INITIALSIZEcxx . . $ `src/asm/basics.c:22:9`!); |
| 224 | Tree_add_tablecxx . . $ Tree_add_table(90ba9e887489f1c).(&pseudo_opcode_treecxx . . $ pseudo_opcode_tree., pseudo_opcodescxx . . $ pseudo_opcodes.); |
| 225 | } |