| 1 | //cxx . . $ `<file>/src/asm/encoding.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 | // Character encoding stuff |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <string.h> |
| 10 | #include "alu.h"cxx . . $ `<file>/src/asm/alu.h`/ |
| 11 | #include "acme.h"cxx . . $ `<file>/src/asm/acme.h`/ |
| 12 | #include "dynabuf.h"cxx . . $ `<file>/src/asm/dynabuf.h`/ |
| 13 | #include "encoding.h"cxx . . $ `<file>/src/asm/encoding.h`/ |
| 14 | #include "global.h"cxx . . $ `<file>/src/asm/global.h`/ |
| 15 | #include "output.h"cxx . . $ `<file>/src/asm/output.h`/ |
| 16 | #include "input.h"cxx . . $ `<file>/src/asm/input.h`/ |
| 17 | #include "tree.h"cxx . . $ `<file>/src/asm/tree.h`/ |
| 18 | |
| 19 | |
| 20 | // Encoder function type definition |
| 21 | typedef char (*encoder_tcxx . . $ encoder_t#)(char) ; |
| 22 | |
| 23 | |
| 24 | // Constants |
| 25 | static const char s_petcxx . . $ s_pet.[] = "pet"; |
| 26 | static const char s_rawcxx . . $ s_raw.[] = "raw"; |
| 27 | static const char s_scrcxx . . $ s_scr.[] = "scr"; |
| 28 | |
| 29 | |
| 30 | // Variables |
| 31 | static char outermost_tablecxx . . $ outermost_table.[256]; // space for encoding table... |
| 32 | static char* loaded_tablecxx . . $ loaded_table. = outermost_tablecxx . . $ outermost_table.; // ...loaded from file |
| 33 | // predefined stuff |
| 34 | static node_tcxx . . $ node_t#* encoder_treecxx . . $ encoder_tree. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; // tree to hold encoders |
| 35 | |
| 36 | |
| 37 | // Functions |
| 38 | |
| 39 | // convert character using current encoding |
| 40 | // Conversion function pointer. No init needed: gets set before each pass. |
| 41 | char (*Encoding_encode_charcxx . . $ Encoding_encode_char.)(char); |
| 42 | |
| 43 | // Insert string(s) |
| 44 | static enum eos_tcxx . . $ eos_t# encode_stringcxx . . $ encode_string(afe1f9e38dcfdfcd).(encoder_tcxx . . $ encoder_t# inner_encoder, char eor) { |
| 45 | encoder_tcxx . . $ encoder_t# outer_encoder = Encoding_encode_charcxx . . $ Encoding_encode_char.;// buffer encoder |
| 46 | |
| 47 | // make given encoder the current one (for ALU-parsed values) |
| 48 | Encoding_encode_charcxx . . $ Encoding_encode_char. = inner_encoder; |
| 49 | do { |
| 50 | if(GotBytecxx . . $ GotByte. == '"') { |
| 51 | // read initial character |
| 52 | GetQuotedBytecxx . . $ GetQuotedByte(6e9c618e4dd2eafc).(); |
| 53 | // send characters until closing quote is reached |
| 54 | while(GotBytecxx . . $ GotByte. && (GotBytecxx . . $ GotByte. != '"')) { |
| 55 | Output_8bcxx . . $ Output_8b(1624ee8275b10bda).(eor ^ Encoding_encode_charcxx . . $ Encoding_encode_char.(GotBytecxx . . $ GotByte.)); |
| 56 | GetQuotedBytecxx . . $ GetQuotedByte(6e9c618e4dd2eafc).(); |
| 57 | } |
| 58 | if(GotBytecxx . . $ GotByte. == CHAR_EOScxx . . $ `src/asm/input.h:48:9`!) |
| 59 | return(AT_EOS_ANYWAYcxx . . $ AT_EOS_ANYWAY.); |
| 60 | // after closing quote, proceed with next char |
| 61 | GetBytecxx . . $ GetByte(6e9c618e4dd2eafc).(); |
| 62 | } else { |
| 63 | // Parse value. No problems with single characters |
| 64 | // because the current encoding is |
| 65 | // temporarily set to the given one. |
| 66 | Output_8bcxx . . $ Output_8b(1624ee8275b10bda).(ALU_any_intcxx . . $ ALU_any_int(da6335ded91d79c2).()); |
| 67 | } |
| 68 | } while(Input_accept_commacxx . . $ Input_accept_comma(c9857a73d68e59ba).()); |
| 69 | Encoding_encode_charcxx . . $ Encoding_encode_char. = outer_encoder; // reactivate buffered encoder |
| 70 | return(ENSURE_EOScxx . . $ ENSURE_EOS.); |
| 71 | } |
| 72 | |
| 73 | // Insert text string (default format) |
| 74 | static enum eos_tcxx . . $ eos_t# PO_textcxx . . $ PO_text(6522f478c9c0d97c).(void) { |
| 75 | return(encode_stringcxx . . $ encode_string(afe1f9e38dcfdfcd).(Encoding_encode_charcxx . . $ Encoding_encode_char., 0)); |
| 76 | } |
| 77 | |
| 78 | // convert raw to raw (do not convert at all) |
| 79 | static char encoder_rawcxx . . $ encoder_raw(e6e5e845b55315fc).(char byte) { |
| 80 | return(byte); |
| 81 | } |
| 82 | |
| 83 | // Insert raw string |
| 84 | static enum eos_tcxx . . $ eos_t# PO_rawcxx . . $ PO_raw(6522f478c9c0d97c).(void) { |
| 85 | return(encode_stringcxx . . $ encode_string(afe1f9e38dcfdfcd).(encoder_rawcxx . . $ encoder_raw(e6e5e845b55315fc)., 0)); |
| 86 | } |
| 87 | |
| 88 | // convert raw to petscii |
| 89 | static char encoder_petcxx . . $ encoder_pet(e6e5e845b55315fc).(char byte) { |
| 90 | if((byte >= 'A') && (byte <= 'Z')) |
| 91 | return((char) (byte | 0x80)); // FIXME - check why SAS-C |
| 92 | if((byte >= 'a') && (byte <= 'z')) // wants these casts. |
| 93 | return((char)(byte - 32)); // There are more below. |
| 94 | return(byte); |
| 95 | } |
| 96 | |
| 97 | // Insert PetSCII string |
| 98 | static enum eos_tcxx . . $ eos_t# PO_petcxx . . $ PO_pet(6522f478c9c0d97c).(void) { |
| 99 | return(encode_stringcxx . . $ encode_string(afe1f9e38dcfdfcd).(encoder_petcxx . . $ encoder_pet(e6e5e845b55315fc)., 0)); |
| 100 | } |
| 101 | |
| 102 | // convert raw to C64 screencode |
| 103 | static char encoder_scrcxx . . $ encoder_scr(e6e5e845b55315fc).(char byte) { |
| 104 | if((byte >= 'a') && (byte <= 'z')) |
| 105 | return((char)(byte - 96)); // shift uppercase down |
| 106 | if((byte >= '[') && (byte <= '_')) |
| 107 | return((char)(byte - 64)); // shift [\]^_ down |
| 108 | if(byte == '`') |
| 109 | return(64); // shift ` down |
| 110 | if(byte == '@') |
| 111 | return(0); // shift @ down |
| 112 | return(byte); |
| 113 | } |
| 114 | |
| 115 | // Insert screencode string |
| 116 | static enum eos_tcxx . . $ eos_t# PO_scrcxx . . $ PO_scr(6522f478c9c0d97c).(void) { |
| 117 | return(encode_stringcxx . . $ encode_string(afe1f9e38dcfdfcd).(encoder_scrcxx . . $ encoder_scr(e6e5e845b55315fc)., 0)); |
| 118 | } |
| 119 | |
| 120 | // Insert screencode string, EOR'd |
| 121 | static enum eos_tcxx . . $ eos_t# PO_scrxorcxx . . $ PO_scrxor(6522f478c9c0d97c).(void) { |
| 122 | intval_tcxx . . $ intval_t# num = ALU_any_intcxx . . $ ALU_any_int(da6335ded91d79c2).(); |
| 123 | |
| 124 | if(Input_accept_commacxx . . $ Input_accept_comma(c9857a73d68e59ba).()) |
| 125 | return(encode_stringcxx . . $ encode_string(afe1f9e38dcfdfcd).(encoder_scrcxx . . $ encoder_scr(e6e5e845b55315fc)., num)); |
| 126 | Throw_errorcxx . . $ Throw_error(85c52e162fed56f9).(exception_syntaxcxx . . $ exception_syntax.); |
| 127 | return(SKIP_REMAINDERcxx . . $ SKIP_REMAINDER.); |
| 128 | } |
| 129 | |
| 130 | // Switch to CBM mode ("!cbm" pseudo opcode) |
| 131 | static enum eos_tcxx . . $ eos_t# PO_cbmcxx . . $ PO_cbm(6522f478c9c0d97c).(void) { |
| 132 | Encoding_encode_charcxx . . $ Encoding_encode_char. = encoder_petcxx . . $ encoder_pet(e6e5e845b55315fc).; |
| 133 | // output deprecation warning |
| 134 | Throw_first_pass_warningcxx . . $ Throw_first_pass_warning(85c52e162fed56f9).("\"!cbm\" is deprecated; use \"!ct pet\" instead."); |
| 135 | return(ENSURE_EOScxx . . $ ENSURE_EOS.); |
| 136 | } |
| 137 | |
| 138 | // |
| 139 | static char encoder_filecxx . . $ encoder_file(e6e5e845b55315fc).(char byte) { |
| 140 | return(loaded_tablecxx . . $ loaded_table.[(unsigned char) byte]); |
| 141 | } |
| 142 | |
| 143 | // read encoding table from file |
| 144 | static enum eos_tcxx . . $ eos_t# user_defined_encodingcxx . . $ user_defined_encoding(6522f478c9c0d97c).(void) { |
| 145 | FILEcxx . . $ FILE#* fd; |
| 146 | char local_table[256], |
| 147 | *buffered_table = loaded_tablecxx . . $ loaded_table.; |
| 148 | encoder_tcxx . . $ encoder_t# buffered_encoder = Encoding_encode_charcxx . . $ Encoding_encode_char.; |
| 149 | |
| 150 | // if file name is missing, don't bother continuing |
| 151 | if(Input_read_filenamecxx . . $ Input_read_filename(efaadfb7dc14930a).(TRUEcxx . . $ `src/asm/config.h:55:9`!)) |
| 152 | return(SKIP_REMAINDERcxx . . $ SKIP_REMAINDER.); |
| 153 | fd = fopencxx . . $ fopen(82ec0787cd37e0a3).(GLOBALDYNABUF_CURRENTcxx . . $ `src/asm/dynabuf.h:23:9`!, FILE_READBINARYcxx . . $ FILE_READBINARY.); |
| 154 | if(fd) { |
| 155 | if(freadcxx . . $ fread(738fc8ac1d711e58).(local_table, sizeof(char), 256, fd) != 256) |
| 156 | Throw_errorcxx . . $ Throw_error(85c52e162fed56f9).("Conversion table incomplete."); |
| 157 | fclosecxx . . $ fclose(89bc1efcdac0ca22).(fd); |
| 158 | } else |
| 159 | Throw_errorcxx . . $ Throw_error(85c52e162fed56f9).(exception_cannot_open_input_filecxx . . $ exception_cannot_open_input_file.); |
| 160 | Encoding_encode_charcxx . . $ Encoding_encode_char. = encoder_filecxx . . $ encoder_file(e6e5e845b55315fc).; // activate new encoding |
| 161 | loaded_tablecxx . . $ loaded_table. = local_table; // activate local table |
| 162 | // If there's a block, parse that and then restore old values |
| 163 | if(Parse_optional_blockcxx . . $ Parse_optional_block(c9857a73d68e59ba).()) |
| 164 | Encoding_encode_charcxx . . $ Encoding_encode_char. = buffered_encoder; |
| 165 | else |
| 166 | // if there's *no* block, the table must be used from now on. |
| 167 | // copy the local table to the "outer" table |
| 168 | memcpycxx . . $ memcpy(cc817cb175f282d).(buffered_table, local_table, 256); |
| 169 | // re-activate "outer" table (it might have been changed by memcpy()) |
| 170 | loaded_tablecxx . . $ loaded_table. = buffered_table; |
| 171 | return(ENSURE_EOScxx . . $ ENSURE_EOS.); |
| 172 | } |
| 173 | |
| 174 | // use one of the pre-defined encodings (raw, pet, scr) |
| 175 | static enum eos_tcxx . . $ eos_t# predefined_encodingcxx . . $ predefined_encoding(6522f478c9c0d97c).(void) { |
| 176 | void* node_body; |
| 177 | char local_table[256], |
| 178 | *buffered_table = loaded_tablecxx . . $ loaded_table.; |
| 179 | encoder_tcxx . . $ encoder_t# buffered_encoder = Encoding_encode_charcxx . . $ Encoding_encode_char.; |
| 180 | |
| 181 | // use one of the pre-defined encodings |
| 182 | if(Input_read_and_lower_keywordcxx . . $ Input_read_and_lower_keyword(b126dc7c1de90089).()) { |
| 183 | // search for tree item |
| 184 | if(Tree_easy_scancxx . . $ Tree_easy_scan(7f10be3e8224db50).(encoder_treecxx . . $ encoder_tree., &node_body, GlobalDynaBufcxx . . $ GlobalDynaBuf.)) |
| 185 | Encoding_encode_charcxx . . $ Encoding_encode_char. = (encoder_tcxx . . $ encoder_t#) node_body;// activate new encoder |
| 186 | else |
| 187 | Throw_errorcxx . . $ Throw_error(85c52e162fed56f9).("Unknown encoding."); |
| 188 | } |
| 189 | loaded_tablecxx . . $ loaded_table. = local_table; // activate local table |
| 190 | // If there's a block, parse that and then restore old values |
| 191 | if(Parse_optional_blockcxx . . $ Parse_optional_block(c9857a73d68e59ba).()) |
| 192 | Encoding_encode_charcxx . . $ Encoding_encode_char. = buffered_encoder; |
| 193 | // re-activate "outer" table |
| 194 | loaded_tablecxx . . $ loaded_table. = buffered_table; |
| 195 | return(ENSURE_EOScxx . . $ ENSURE_EOS.); |
| 196 | } |
| 197 | |
| 198 | // Set current encoding ("!convtab" pseudo opcode) |
| 199 | static enum eos_tcxx . . $ eos_t# PO_convtabcxx . . $ PO_convtab(6522f478c9c0d97c).(void) { |
| 200 | if((GotBytecxx . . $ GotByte. == '<') || (GotBytecxx . . $ GotByte. == '"')) |
| 201 | return(user_defined_encodingcxx . . $ user_defined_encoding(6522f478c9c0d97c).()); |
| 202 | else |
| 203 | return(predefined_encodingcxx . . $ predefined_encoding(6522f478c9c0d97c).()); |
| 204 | } |
| 205 | |
| 206 | // pseudo opcode table |
| 207 | static node_tcxx . . $ node_t# pseudo_opcodescxx . . $ pseudo_opcodes.[] = { |
| 208 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!(s_cbm, PO_cbm), |
| 209 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("ct", PO_convtab), |
| 210 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("convtab", PO_convtab), |
| 211 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!(s_pet, PO_pet), |
| 212 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!(s_raw, PO_raw), |
| 213 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!(s_scr, PO_scr), |
| 214 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!(s_scrxor, PO_scrxor), |
| 215 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!("text", PO_text), |
| 216 | PREDEFLASTcxx . . $ `src/asm/tree.h:16:9`!("tx", PO_text), |
| 217 | // ^^^^ this marks the last element |
| 218 | }; |
| 219 | |
| 220 | // keywords for "!convtab" pseudo opcode |
| 221 | static node_tcxx . . $ node_t# encoderscxx . . $ encoders.[] = { |
| 222 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!(s_pet, encoder_pet), |
| 223 | PREDEFNODEcxx . . $ `src/asm/tree.h:15:9`!(s_raw, encoder_raw), |
| 224 | PREDEFLASTcxx . . $ `src/asm/tree.h:16:9`!(s_scr, encoder_scr), |
| 225 | // ^^^^ this marks the last element |
| 226 | }; |
| 227 | |
| 228 | |
| 229 | // Exported functions |
| 230 | |
| 231 | // register pseudo opcodes and build keyword tree for encoders |
| 232 | void Encoding_initcxx . . $ Encoding_init(49f6e7a06ebc5aa8).(void) { |
| 233 | Tree_add_tablecxx . . $ Tree_add_table(90ba9e887489f1c).(&encoder_treecxx . . $ encoder_tree., encoderscxx . . $ encoders.); |
| 234 | Tree_add_tablecxx . . $ Tree_add_table(90ba9e887489f1c).(&pseudo_opcode_treecxx . . $ pseudo_opcode_tree., pseudo_opcodescxx . . $ pseudo_opcodes.); |
| 235 | } |
| 236 | |
| 237 | // Set "raw" as default encoding |
| 238 | void Encoding_passinitcxx . . $ Encoding_passinit(49f6e7a06ebc5aa8).(void) { |
| 239 | Encoding_encode_charcxx . . $ Encoding_encode_char. = encoder_rawcxx . . $ encoder_raw(e6e5e845b55315fc).; |
| 240 | } |