Debian Codegraph @

src/asm/basics.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 // basic assembly stuff
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include "config.h"
11 #include "cpu.h"
12 #include "basics.h"
13 #include "alu.h"
14 #include "dynabuf.h"
15 #include "input.h"
16 #include "global.h"
17 #include "output.h"
18 #include "tree.h"
19
20
21 // Constants
22 #define USERMSG_DYNABUF_INITIALSIZE 80
23 static const char s_08[] = "08";
24 #define s_8 (s_08+1) // Yes, I know I'm sick
25 #define s_16 (s_65816+3) // Yes, I know I'm sick
26
27
28 // Variables
29 static dynabuf_t* 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_t output_objects(void (*fn)(intval_t)) {
36 do
37 fn(ALU_any_int());
38 while(Input_accept_comma());
39 return(ENSURE_EOS);
40 }
41
42 // Insert 8-bit values ("!08" / "!8" / "!by" / "!byte" pseudo opcode)
43 static enum eos_t PO_08(void) {
44 return(output_objects(Output_8b));
45 }
46
47 // Insert 16-bit values ("!16" / "!wo" / "!word" pseudo opcode)
48 static enum eos_t PO_16(void) {
49 return(output_objects(Output_16b));
50 }
51
52 // Insert 24-bit values ("!24" pseudo opcode)
53 static enum eos_t PO_24(void) {
54 return(output_objects(Output_24b));
55 }
56
57 // Insert 32-bit values ("!32" pseudo opcode)
58 static enum eos_t PO_32(void) {
59 return(output_objects(Output_32b));
60 }
61
62 // Include binary file
63 static enum eos_t PO_binary(void) {
64 FILE* fd;
65 int byte;
66 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_filename(TRUE))
71 return(SKIP_REMAINDER);
72 // try to open file
73 fd = fopen(GLOBALDYNABUF_CURRENT, FILE_READBINARY);
74 if(fd == NULL) {
75 Throw_error(exception_cannot_open_input_file);
76 return(SKIP_REMAINDER);
77 }
78 // read optional arguments
79 if(Input_accept_comma()) {
80 if(ALU_optional_defined_int(&size)
81 && (size <0))
82 Throw_serious_error("Negative size argument.");
83 if(Input_accept_comma())
84 ALU_optional_defined_int(&skip);// read skip
85 }
86 // check whether including is a waste of time
87 if((size >= 0) && (pass_undefined_count || pass_real_errors))
88 Output_fake(size); // really including is useless anyway
89 else {
90 // really insert file
91 fseek(fd, skip, SEEK_SET); // set read pointer
92 // if "size" non-negative, read "size" bytes.
93 // otherwise, read until EOF.
94 while(size != 0) {
95 byte = getc(fd);
96 if(byte == EOF)
97 break;
98 Output_byte(byte);
99 size--;
100 }
101 // if more should have been read, warn and add padding
102 if(size > 0) {
103 Throw_warning("Padding with zeroes.");
104 do
105 Output_byte(0);
106 while(--size);
107 }
108 }
109 fclose(fd);
110 // if verbose, produce some output
111 if((pass_count == 0) && (Process_verbosity > 1))
112 printf("Loaded %d ($%x) bytes from file offset %ld ($%lx).\n",
113 CPU_2add, CPU_2add, skip, skip);
114 return(ENSURE_EOS);
115 }
116
117 // Reserve space by sending bytes of given value ("!fi" / "!fill" pseudo opcode)
118 static enum eos_t PO_fill(void) {
119 intval_t fill = FILLVALUE_FILL,
120 size = ALU_defined_int();
121
122 if(Input_accept_comma())
123 fill = ALU_any_int();
124 while(size--)
125 Output_8b(fill);
126 return(ENSURE_EOS);
127 }
128
129 // show user-defined message
130 static enum eos_t throw_string(const char prefix[], void (*fn)(const char*)) {
131 result_t result;
132
133 DYNABUF_CLEAR(user_message);
134 DynaBuf_add_string(user_message, prefix);
135 do {
136 if(GotByte == '"') {
137 // parse string
138 GetQuotedByte(); // read initial character
139 // send characters until closing quote is reached
140 while(GotByte && (GotByte != '"')) {
141 DYNABUF_APPEND(user_message, GotByte);
142 GetQuotedByte();
143 }
144 if(GotByte == CHAR_EOS)
145 return(AT_EOS_ANYWAY);
146 // after closing quote, proceed with next char
147 GetByte();
148 } else {
149 // parse value
150 ALU_any_result(&result);
151 if(result.flags & MVALUE_IS_FP) {
152 // floating point
153 if(result.flags & MVALUE_DEFINED)
154 DynaBuf_add_double(
155 user_message,
156 result.val.fpval);
157 else
158 DynaBuf_add_string(
159 user_message,
160 "<UNDEFINED FLOAT>");
161 } else {
162 // integer
163 if(result.flags & MVALUE_DEFINED)
164 DynaBuf_add_signed_long(
165 user_message,
166 result.val.intval);
167 else
168 DynaBuf_add_string(
169 user_message,
170 "<UNDEFINED INT>");
171 }
172 }
173 } while(Input_accept_comma());
174 DynaBuf_append(user_message, '\0');
175 fn(user_message->buffer);
176 return(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_t PO_warn(void) {
186 return(throw_string("!warn: ", Throw_warning));
187 }
188
189 // throw error as given in source code
190 static enum eos_t PO_error(void) {
191 return(throw_string("!error: ", Throw_error));
192 }
193
194 // throw serious error as given in source code
195 static enum eos_t PO_serious(void) {
196 return(throw_string("!serious: ", Throw_serious_error));
197 }
198
199 // pseudo ocpode table
200 static node_t pseudo_opcodes[] = {
201 PREDEFNODE(s_08, PO_08),
202 PREDEFNODE(s_8, PO_08),
203 PREDEFNODE("by", PO_08),
204 PREDEFNODE("byte", PO_08),
205 PREDEFNODE(s_16, PO_16),
206 PREDEFNODE("wo", PO_16),
207 PREDEFNODE("word", PO_16),
208 PREDEFNODE("24", PO_24),
209 PREDEFNODE("32", PO_32),
210 PREDEFNODE("bin", PO_binary),
211 PREDEFNODE("binary", PO_binary),
212 PREDEFNODE("fi", PO_fill),
213 PREDEFNODE("fill", PO_fill),
214 // PREDEFNODE("print", PO_print),
215 PREDEFNODE("warn", PO_warn),
216 PREDEFNODE(s_error, PO_error),
217 PREDEFLAST("serious", PO_serious),
218 // ^^^^ this marks the last element
219 };
220
221 // register pseudo opcodes and create dynamic buffer
222 void Basics_init(void) {
223 user_message = DynaBuf_create(USERMSG_DYNABUF_INITIALSIZE);
224 Tree_add_table(&pseudo_opcode_tree, pseudo_opcodes);
225 }