asar coverage - build #89


src/asar/
File: src/asar/main.cpp
Date: 2024-01-19 15:22:25
Lines:
541/565
95.8%
Functions:
37/38
97.4%
Branches:
507/704
72.0%

Line Branch Exec Source
1 // "because satanism is best defeated by summoning a bigger satan"
2 // ~Alcaro, 2019 (discussing Asar)
3 #include "addr2line.h"
4 #include "asar.h"
5 #include "virtualfile.h"
6 #include "platform/file-helpers.h"
7 #include "assembleblock.h"
8 #include "asar_math.h"
9 #include "macro.h"
10 #include <ctime>
11 // randomdude999: remember to also update the .rc files (in res/windows/) when changing this.
12 // Couldn't find a way to automate this without shoving the version somewhere in the CMake files
13 const int asarver_maj=2;
14 const int asarver_min=0;
15 const int asarver_bug=0;
16 const bool asarver_beta=true;
17
18 #ifdef _I_RELEASE
19 extern char blockbetareleases[(!asarver_beta)?1:-1];
20 #endif
21 #ifdef _I_DEBUG
22 extern char blockreleasedebug[(asarver_beta)?1:-1];
23 #endif
24
25 unsigned const char * romdata_r;
26 int romlen_r;
27
28 int pass;
29
30 int optimizeforbank=-1;
31 int optimize_dp = optimize_dp_flag::NONE;
32 int dp_base = 0;
33 int optimize_address = optimize_address_flag::DEFAULT;
34
35 autoarray<callstack_entry> callstack;
36
37 bool errored=false;
38 bool ignoretitleerrors=false;
39
40 volatile int recursioncount=0;
41
42 virtual_filesystem* filesystem = nullptr;
43
44 AddressToLineMapping addressToLineMapping;
45
46 422 int get_version_int()
47 {
48 422 return asarver_maj * 10000 + asarver_min * 100 + asarver_bug;
49 }
50
51 58 bool setmapper()
52 {
53 int maxscore=-99999;
54 mapper_t bestmap=lorom;
55 58 mapper_t maps[]={lorom, hirom, exlorom, exhirom};
56
2/2
✓ Branch 0 taken 232 times.
✓ Branch 1 taken 58 times.
290 for (size_t mapid=0;mapid<sizeof(maps)/sizeof(maps[0]);mapid++)
57 {
58 232 mapper=maps[mapid];
59 int score=0;
60 int highbits=0;
61 bool foundnull=false;
62
2/2
✓ Branch 0 taken 4872 times.
✓ Branch 1 taken 232 times.
5104 for (int i=0;i<21;i++)
63 {
64 4872 unsigned char c=romdata[snestopc(0x00FFC0+i)];
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4872 times.
4872 if (foundnull && c) score-=4;//according to some documents, NUL terminated names are possible - but they shouldn't appear in the middle of the name
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4872 times.
4872 if (c>=128) highbits++;
67
2/2
✓ Branch 0 taken 840 times.
✓ Branch 1 taken 4032 times.
4872 else if (is_upper(c)) score+=3;
68
2/2
✓ Branch 0 taken 336 times.
✓ Branch 1 taken 3696 times.
4032 else if (c==' ') score+=2;
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3696 times.
3696 else if (is_digit(c)) score+=1;
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3696 times.
3696 else if (is_lower(c)) score+=1;
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3696 times.
3696 else if (c=='-') score+=1;
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3696 times.
3696 else if (!c) foundnull=true;
73 else score-=3;
74 }
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 232 times.
232 if (highbits>0 && highbits<=14) score-=21;//high bits set on some, but not all, bytes = unlikely to be a ROM
76
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 174 times.
232 if ((romdata[snestopc(0x00FFDE)]^romdata[snestopc(0x00FFDC)])!=0xFF ||
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 (romdata[snestopc(0x00FFDF)]^romdata[snestopc(0x00FFDD)])!=0xFF) score=-99999;//checksum doesn't match up to 0xFFFF? Not a ROM.
78 //too lazy to check the real checksum
79
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 174 times.
232 if (score>maxscore)
80 {
81 maxscore=score;
82 bestmap=mapper;
83 }
84 }
85 58 mapper=bestmap;
86
87 //detect oddball mappers
88 58 int mapperbyte=romdata[snestopc(0x00FFD5)];
89 58 int romtypebyte=romdata[snestopc(0x00FFD6)];
90
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
58 if (mapper==lorom)
91 {
92
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
58 if (mapperbyte==0x23 && (romtypebyte==0x32 || romtypebyte==0x34 || romtypebyte==0x35)) mapper=sa1rom;
93 }
94 58 return (maxscore>=0);
95 }
96
97
98 bool simple_callstacks = true;
99
100 // Shortens target_path to a relative path, but only if it resides
101 // within base_path or a child directory of it.
102 17421 string shorten_to_relative_path(const char* base_path, const char* target_path)
103 {
104
2/2
✓ Branch 0 taken 17415 times.
✓ Branch 1 taken 6 times.
17421 if (stribegin(target_path, base_path)) target_path += strlen(base_path);
105 17421 return target_path;
106 }
107
108 17421 string get_top_level_directory()
109 {
110 1962 string top_level_file_dir;
111
1/2
✓ Branch 0 taken 17421 times.
✗ Branch 1 not taken.
17421 for (int i = 0; i < callstack.count; ++i)
112 {
113
1/2
✓ Branch 0 taken 17421 times.
✗ Branch 1 not taken.
17421 if (callstack[i].type == callstack_entry_type::FILE)
114 {
115
1/2
✓ Branch 0 taken 17421 times.
✗ Branch 1 not taken.
17421 top_level_file_dir = dir(callstack[i].content);
116 17421 break;
117 }
118 }
119 17421 return top_level_file_dir;
120 }
121
122 17337 string generate_call_details_string(const char* current_block, const char* current_call, int indentation, bool add_lines)
123 {
124 1920 string e;
125
2/2
✓ Branch 0 taken 17289 times.
✓ Branch 1 taken 48 times.
17337 if (current_block != nullptr || current_call != nullptr)
126 {
127 1896 string indent;
128
2/2
✓ Branch 0 taken 5548 times.
✓ Branch 1 taken 11741 times.
17289 if (add_lines) indent += "|";
129
2/2
✓ Branch 0 taken 69156 times.
✓ Branch 1 taken 17289 times.
86445 for (; indentation > 0; --indentation) indent += " ";
130
131
2/2
✓ Branch 0 taken 17163 times.
✓ Branch 1 taken 126 times.
34452 if (current_block != nullptr) e += STR "\n"+indent+"in block: ["+current_block+"]";
132
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 17163 times.
17415 if (current_call != nullptr) e += STR "\n"+indent+"in macro call: [%"+current_call+"]";
133 17289 }
134 17337 return e;
135 }
136
137 17421 string get_pretty_filename(const char* current_file)
138 {
139 // RPG Hacker: One could make an argument that we shouldn't shorten paths
140 // here, since some IDEs support jumping to files by double-clicking their
141 // paths. However, AFAIK, no IDE supports this for Asar yet, and if it's
142 // ever desired, we could just make it a command line option. Until then,
143 // I think it's more important to optimize for pretty command line display.
144
1/2
✓ Branch 0 taken 1826 times.
✗ Branch 1 not taken.
17421 return shorten_to_relative_path(get_top_level_directory(), current_file);
145 }
146
147 6325 string generate_filename_and_line(const char* current_file, int current_line_no)
148 {
149 7239 return STR current_file
150
6/6
✓ Branch 0 taken 6289 times.
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 6289 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 6289 times.
✓ Branch 5 taken 36 times.
12650 + (current_line_no>=0?STR ":"+dec(current_line_no+1):"");
151 }
152
153 5548 string format_stack_line(const printable_callstack_entry& entry, int stack_frame_index)
154 {
155 524 string indent = "\n| ";
156 5548 indent += dec(stack_frame_index);
157 5548 indent += ": ";
158 // RPG Hacker: We'll probably never have a call stack in the
159 // hundreds even, so this very specific, lazy solution suffices.
160
2/2
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 5300 times.
5548 if (stack_frame_index < 100) indent += " ";
161
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 180 times.
248 if (stack_frame_index < 10) indent += " ";
162 return indent
163
1/2
✓ Branch 0 taken 524 times.
✗ Branch 1 not taken.
11096 + generate_filename_and_line(entry.prettypath, entry.lineno)
164 5548 + entry.details;
165 5548 }
166
167 16644 void push_stack_line(autoarray<printable_callstack_entry>* out, const char* current_file, const char* current_block, const char* current_call, int current_line_no, int indentation, bool add_lines)
168 {
169 1572 printable_callstack_entry new_entry;
170 new_entry.fullpath = current_file;
171
1/2
✓ Branch 0 taken 16644 times.
✗ Branch 1 not taken.
16644 new_entry.prettypath = get_pretty_filename(current_file);
172 16644 new_entry.lineno = current_line_no;
173
1/2
✓ Branch 0 taken 1572 times.
✗ Branch 1 not taken.
33288 new_entry.details = generate_call_details_string(current_block, current_call, indentation, add_lines).raw();
174 16644 out->append(new_entry);
175 16644 }
176
177 694 void get_current_line_details(string* location, string* details, bool exclude_block)
178 {
179 const char* current_file = nullptr;
180 const char* current_block = nullptr;
181 const char* current_call = nullptr;
182 int current_line_no = -1;
183
2/2
✓ Branch 0 taken 2328 times.
✓ Branch 1 taken 1 times.
2329 for (int i = callstack.count-1; i >= 0 ; --i)
184 {
185
3/5
✓ Branch 0 taken 693 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 657 times.
✓ Branch 3 taken 978 times.
✗ Branch 4 not taken.
2328 switch (callstack[i].type)
186 {
187 case callstack_entry_type::FILE:
188 current_file = callstack[i].content;
189
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 671 times.
693 if (exclude_block) current_block = nullptr;
190
1/2
✓ Branch 0 taken 233 times.
✗ Branch 1 not taken.
693 *location = generate_filename_and_line(get_pretty_filename(current_file), current_line_no);
191 693 *details = generate_call_details_string(current_block, current_call, 4, false);
192 693 return;
193 case callstack_entry_type::MACRO_CALL:
194 if (current_call == nullptr) current_call = callstack[i].content;
195 break;
196 657 case callstack_entry_type::LINE:
197
2/2
✓ Branch 0 taken 162 times.
✓ Branch 1 taken 495 times.
657 if (current_block == nullptr && current_call == nullptr) current_block = callstack[i].content;
198
1/2
✓ Branch 0 taken 657 times.
✗ Branch 1 not taken.
1314 if (current_line_no == -1) current_line_no = callstack[i].lineno;
199 break;
200 978 case callstack_entry_type::BLOCK:
201
2/2
✓ Branch 0 taken 495 times.
✓ Branch 1 taken 483 times.
978 if (current_block == nullptr) current_block = callstack[i].content;
202 break;
203 }
204 }
205 *location = "";
206 *details = "";
207 }
208
209 694 void get_full_printable_callstack(autoarray<printable_callstack_entry>* out, int indentation, bool add_lines)
210 {
211 694 out->reset();
212 const char* current_file = nullptr;
213 const char* current_block = nullptr;
214 const char* current_call = nullptr;
215 int current_line_no = -1;
216
2/2
✓ Branch 0 taken 69030 times.
✓ Branch 1 taken 694 times.
69724 for (int i = 0; i < callstack.count; ++i)
217 {
218
4/5
✓ Branch 0 taken 17337 times.
✓ Branch 1 taken 126 times.
✓ Branch 2 taken 17301 times.
✓ Branch 3 taken 34266 times.
✗ Branch 4 not taken.
69030 switch (callstack[i].type)
219 {
220 17337 case callstack_entry_type::FILE:
221
2/2
✓ Branch 0 taken 16644 times.
✓ Branch 1 taken 693 times.
17337 if (current_file != nullptr)
222 {
223 16644 push_stack_line(out, current_file, current_block, current_call, current_line_no, indentation, add_lines);
224 }
225 current_file = callstack[i].content;
226 current_block = nullptr;
227 current_call = nullptr;
228 current_line_no = -1;
229 17337 break;
230 126 case callstack_entry_type::MACRO_CALL:
231 current_block = nullptr;
232 current_call = callstack[i].content;
233 126 break;
234 case callstack_entry_type::LINE:
235 17301 current_line_no = callstack[i].lineno;
236 current_block = callstack[i].content;
237 17301 break;
238 case callstack_entry_type::BLOCK:
239 current_block = callstack[i].content;
240 34266 break;
241 }
242 }
243 694 }
244
245 230 string get_full_callstack()
246 {
247 115 autoarray<printable_callstack_entry> printable_stack;
248
1/2
✓ Branch 0 taken 230 times.
✗ Branch 1 not taken.
230 get_full_printable_callstack(&printable_stack, 12, true);
249
250 115 string e;
251
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 180 times.
230 if (printable_stack.count > 0)
252 {
253 50 e += "\nFull call stack:";
254
2/2
✓ Branch 0 taken 5548 times.
✓ Branch 1 taken 50 times.
5598 for (int i = printable_stack.count-1; i >= 0; --i)
255 {
256
1/2
✓ Branch 0 taken 524 times.
✗ Branch 1 not taken.
5548 e += format_stack_line(printable_stack[i], i);
257 }
258 }
259 230 return e;
260 230 }
261
262 // RPG Hacker: This function essetially replicates classic Asar behavior
263 // of only printing a single macro call below the current level.
264 464 string get_simple_callstack()
265 {
266 int i;
267 const char* current_call = nullptr;
268
2/2
✓ Branch 0 taken 45688 times.
✓ Branch 1 taken 380 times.
46068 for (i = callstack.count-1; i >= 0 ; --i)
269 {
270
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 45604 times.
45688 if (callstack[i].type == callstack_entry_type::MACRO_CALL)
271 {
272 current_call = callstack[i].content;
273 84 break;
274 }
275 }
276
277 const char* current_file = nullptr;
278 int current_line_no = -1;
279
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 380 times.
464 if (current_call != nullptr)
280 {
281 bool stop = false;
282
1/2
✓ Branch 0 taken 336 times.
✗ Branch 1 not taken.
336 for (int j = i-1; j >= 0 ; --j)
283 {
284
3/4
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
✓ Branch 3 taken 168 times.
336 switch (callstack[j].type)
285 {
286 84 case callstack_entry_type::FILE:
287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
84 if (current_file != nullptr)
288 {
289 stop = true;
290 break;
291 }
292 current_file = callstack[j].content;
293 84 break;
294 case callstack_entry_type::MACRO_CALL:
295 stop = true;
296 break;
297 84 case callstack_entry_type::LINE:
298
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
168 if (current_line_no == -1) current_line_no = callstack[j].lineno;
299 break;
300 case callstack_entry_type::BLOCK:
301 break;
302 }
303
304
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 84 times.
336 if (current_file != nullptr && current_line_no != -1) stop = true;
305
306
1/2
✓ Branch 0 taken 252 times.
✗ Branch 1 not taken.
252 if (stop) break;
307 }
308 }
309
310 234 string e;
311
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 380 times.
464 if (current_call != nullptr && current_file != nullptr)
312 {
313
2/4
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
168 e += STR "\n called from: " + generate_filename_and_line(get_pretty_filename(current_file), current_line_no)
314 168 + ": [%" + current_call + "]";
315 }
316 464 return e;
317 }
318
319 694 string get_callstack()
320 {
321
2/2
✓ Branch 0 taken 464 times.
✓ Branch 1 taken 230 times.
694 if (simple_callstacks)
322 464 return get_simple_callstack();
323 else
324 230 return get_full_callstack();
325 }
326
327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 asar_error_id vfile_error_to_error_id(virtual_file_error vfile_error)
328 {
329 switch (vfile_error)
330 {
331 case vfe_doesnt_exist:
332 return error_id_file_not_found;
333 case vfe_access_denied:
334 return error_id_failed_to_open_file_access_denied;
335 case vfe_not_regular_file:
336 return error_id_failed_to_open_not_regular_file;
337 case vfe_unknown:
338 case vfe_none:
339 case vfe_num_errors:
340 return error_id_failed_to_open_file;
341 }
342
343 return error_id_failed_to_open_file;
344 }
345
346 54 virtual_file_error asar_get_last_io_error()
347 {
348
1/2
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
54 if (filesystem != nullptr)
349 {
350 54 return filesystem->get_last_error();
351 }
352
353 return vfe_unknown;
354 }
355
356 static bool freespaced;
357 1678 static int getlenforlabel(snes_label thislabel, bool exists)
358 {
359 1678 unsigned int bank = thislabel.pos>>16;
360 1678 unsigned int word = thislabel.pos&0xFFFF;
361 1678 unsigned int relaxed_bank = optimizeforbank < 0 ? 0 : optimizeforbank;
362
2/2
✓ Branch 0 taken 1402 times.
✓ Branch 1 taken 276 times.
1678 if (!exists)
363 {
364 return 2;
365 }
366
5/6
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 1366 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 18 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 18 times.
1402 else if((optimize_dp == optimize_dp_flag::RAM) && bank == 0x7E && (word-dp_base < 0x100))
367 {
368 return 1;
369 }
370
6/8
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 1306 times.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 78 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 24 times.
✓ Branch 7 taken 54 times.
1384 else if(optimize_dp == optimize_dp_flag::ALWAYS && (bank == 0x7E || !(bank & 0x40)) && (word-dp_base < 0x100))
371 {
372 return 1;
373 }
374
4/4
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 1294 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 18 times.
1330 else if (optimize_address == optimize_address_flag::RAM && bank == 0x7E && word < 0x2000)
375 {
376 return 2;
377 }
378
8/10
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 1240 times.
✓ Branch 2 taken 54 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 54 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 54 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 54 times.
✓ Branch 9 taken 18 times.
1312 else if (optimize_address == optimize_address_flag::MIRRORS && (bank == relaxed_bank || (!(bank & 0x40) && !(relaxed_bank & 0x40))) && word < 0x2000)
379 {
380 return 2;
381 }
382
5/8
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 1240 times.
✓ Branch 2 taken 54 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 54 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 54 times.
✗ Branch 7 not taken.
1294 else if (optimize_address == optimize_address_flag::MIRRORS && !(bank & 0x40) && !(relaxed_bank & 0x40) && word < 0x8000)
383 {
384 return 2;
385 }
386
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 1258 times.
1294 else if (optimizeforbank>=0)
387 {
388
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 if (thislabel.freespace_id > 0) return 3;
389
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 else if (bank==(unsigned int)optimizeforbank) return 2;
390 36 else return 3;
391 }
392
3/4
✓ Branch 0 taken 1144 times.
✓ Branch 1 taken 114 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1144 times.
1258 else if (thislabel.freespace_id > 0 || freespaceid > 0)
393 {
394 // TODO: check whether they're pinned to the same bank
395
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 54 times.
114 if (thislabel.freespace_id != freespaceid) return 3;
396 60 else return 2;
397 }
398
2/2
✓ Branch 0 taken 1084 times.
✓ Branch 1 taken 60 times.
1144 else if ((int)bank != snespos >> 16){ return 3; }
399 1084 else { return 2;}
400 }
401
402
403 9813 bool is_hex_constant(const char* str){
404
2/2
✓ Branch 0 taken 8337 times.
✓ Branch 1 taken 1476 times.
9813 if (*str=='$')
405 {
406 8337 str++;
407
2/2
✓ Branch 0 taken 29538 times.
✓ Branch 1 taken 8337 times.
37875 while(is_xdigit(*str)) {
408 29538 str++;
409 }
410
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8337 times.
8337 if(*str=='\0'){
411 return true;
412 }
413 }
414 return false;
415 }
416
417 9021 int getlen(const char * orgstr, bool optimizebankextraction)
418 {
419 9021 const char * str=orgstr;
420 9021 freespaced=false;
421
422 9021 const char* posneglabel = str;
423 9021 string posnegname = posneglabelname(&posneglabel, false);
424
425
2/2
✓ Branch 0 taken 8835 times.
✓ Branch 1 taken 186 times.
9021 if (posnegname.length() > 0)
426 {
427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 186 times.
186 if (*posneglabel != '\0') goto notposneglabel;
428
429
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 62 times.
310 if (!pass) return 2;
430 64 snes_label label_data;
431 // RPG Hacker: Umm... what kind of magic constant is this?
432 124 label_data.pos = 31415926;
433
1/2
✓ Branch 0 taken 124 times.
✗ Branch 1 not taken.
124 bool found = labelval(posnegname, &label_data);
434 124 return getlenforlabel(label_data, found);
435 }
436 8835 notposneglabel:
437 int len=0;
438
2/2
✓ Branch 0 taken 9255 times.
✓ Branch 1 taken 8835 times.
18090 while (*str)
439 {
440 int thislen=0;
441 9255 bool maybebankextraction=(str==orgstr);
442
2/2
✓ Branch 0 taken 7095 times.
✓ Branch 1 taken 2160 times.
9255 if (*str=='$')
443 {
444 7095 str++;
445 int i;
446
2/2
✓ Branch 0 taken 28062 times.
✓ Branch 1 taken 7095 times.
35157 for (i=0;is_xdigit(str[i]);i++);
447 //if (i&1) warn(S dec(i)+"-digit hex value");//blocked in getnum instead
448 7095 thislen=(i+1)/2;
449 7095 str+=i;
450 }
451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2160 times.
2160 else if (*str=='%')
452 {
453 str++;
454 int i;
455 for (i=0;str[i]=='0' || str[i]=='1';i++);
456 //if (i&7) warn(S dec(i)+"-digit binary value");
457 thislen=(i+7)/8;
458 str+=i;
459 }
460
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2160 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2160 else if (str[0]=='\'' && str[2]=='\'')
461 {
462 thislen=1;
463 str+=3;
464 }
465
2/2
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 1854 times.
2160 else if (is_digit(*str))
466 {
467 306 int val=strtol(str, const_cast<char**>(&str), 10);
468
1/2
✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
306 if (val>=0) thislen=1;
469
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 54 times.
306 if (val>=256) thislen=2;
470
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (val>=65536) thislen=3;
471 }
472
4/6
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 1554 times.
✓ Branch 2 taken 300 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 300 times.
✗ Branch 5 not taken.
1854 else if (is_ualpha(*str) || *str=='.' || *str=='?')
473 {
474 777 snes_label thislabel;
475
1/2
✓ Branch 0 taken 1554 times.
✗ Branch 1 not taken.
1554 bool exists=labelval(&str, &thislabel);
476 1554 thislen=getlenforlabel(thislabel, exists);
477 }
478 300 else str++;
479
2/2
✓ Branch 0 taken 933 times.
✓ Branch 1 taken 8322 times.
9255 if (optimizebankextraction && maybebankextraction &&
480
3/6
✓ Branch 0 taken 933 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 933 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 933 times.
✗ Branch 5 not taken.
933 (!strcmp(str, ">>16") || !strcmp(str, "/65536") || !strcmp(str, "/$10000")))
481 return 1;
482 if (thislen>len) len=thislen;
483 }
484 return len;
485 9021 }
486
487 struct strcompare {
488 bool operator() (const char * lhs, const char * rhs) const
489 {
490 return strcmp(lhs, rhs)<0;
491 }
492 };
493
494 struct stricompare {
495 bool operator() (const char * lhs, const char * rhs) const
496 {
497 return stricmp(lhs, rhs)<0;
498 }
499 };
500
501 struct sourcefile {
502 char *data;
503 char** contents;
504 int numlines;
505 };
506
507 static assocarr<sourcefile> filecontents;
508 assocarr<string> defines;
509 // needs to be separate because defines is reset between parsing arguments and patching
510 assocarr<string> clidefines;
511 assocarr<string> builtindefines;
512
513 5066 bool validatedefinename(const char * name)
514 {
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5066 times.
5066 if (!name[0]) return false;
516
2/2
✓ Branch 0 taken 52624 times.
✓ Branch 1 taken 5066 times.
57690 for (int i = 0;name[i];i++)
517 {
518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52624 times.
52624 if (!is_ualnum(name[i])) return false;
519 }
520
521 return true;
522 }
523
524 177598 void resolvedefines(string& out, const char * start)
525 {
526 82142 recurseblock rec;
527 const char * here=start;
528
2/2
✓ Branch 0 taken 146432 times.
✓ Branch 1 taken 31160 times.
177592 if (!strchr(here, '!'))
529 {
530 146432 out += here;
531 return;
532 }
533
2/2
✓ Branch 0 taken 212600 times.
✓ Branch 1 taken 31070 times.
243670 while (*here)
534 {
535
4/4
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 212456 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 126 times.
212600 if (here[0] == '\\' && here[1] == '\\')
536 {
537 // allow using \\ as escape sequence
538
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (in_macro_def > 0) out += "\\";
539 18 out += "\\";
540 18 here += 2;
541 }
542
3/4
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 212456 times.
✓ Branch 2 taken 126 times.
✗ Branch 3 not taken.
212582 else if (here[0] == '\\' && here[1] == '!')
543 {
544 // allow using \! to escape !
545
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 90 times.
126 if (in_macro_def > 0) out += "\\";
546 126 out+="!";
547 126 here += 2;
548 }
549
2/2
✓ Branch 0 taken 33422 times.
✓ Branch 1 taken 179034 times.
212456 else if (*here=='!')
550 {
551
9/10
✓ Branch 0 taken 16227 times.
✓ Branch 1 taken 17195 times.
✓ Branch 2 taken 9981 times.
✓ Branch 3 taken 6246 times.
✓ Branch 4 taken 7581 times.
✓ Branch 5 taken 2400 times.
✓ Branch 6 taken 324 times.
✓ Branch 7 taken 7257 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 324 times.
33422 bool first=(here==start || (here>=start+4 && here[-1]==' ' && here[-2]==':' && here[-3]==' '));//check if it's the start of a block
552 16729 string defname;
553 33422 here++;
554
555 int depth = 0;
556
2/2
✓ Branch 0 taken 342 times.
✓ Branch 1 taken 33422 times.
33764 for (const char* depth_str = here; *depth_str=='^'; depth_str++)
557 {
558 342 depth++;
559 }
560 33422 here += depth;
561
562
2/2
✓ Branch 0 taken 3960 times.
✓ Branch 1 taken 29462 times.
33422 if (depth != in_macro_def)
563 {
564 3960 out += '!';
565
2/2
✓ Branch 0 taken 198 times.
✓ Branch 1 taken 3960 times.
4158 for (int i=0; i < depth; ++i) out += '^';
566
3/4
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 3906 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 54 times.
3960 if (depth > in_macro_def) asar_throw_error(0, error_type_line, error_id_invalid_depth_resolve, "define", "define", depth, in_macro_def);
567 continue;
568 3906 }
569
570
2/2
✓ Branch 0 taken 324 times.
✓ Branch 1 taken 29138 times.
29462 if (*here=='{')
571 {
572 324 here++;
573 162 string unprocessedname;
574 int braces=1;
575 while (true)
576 {
577
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 3240 times.
3456 if (*here=='{') braces++;
578
2/2
✓ Branch 0 taken 540 times.
✓ Branch 1 taken 2916 times.
3456 if (*here=='}') braces--;
579
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3456 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3456 if (!*here) asar_throw_error(0, error_type_line, error_id_mismatched_braces);
580
2/2
✓ Branch 0 taken 3132 times.
✓ Branch 1 taken 324 times.
3456 if (!braces) break;
581 3132 unprocessedname+=*here++;
582 }
583
1/2
✓ Branch 0 taken 324 times.
✗ Branch 1 not taken.
324 here++;
584
1/2
✓ Branch 0 taken 324 times.
✗ Branch 1 not taken.
324 resolvedefines(defname, unprocessedname);
585
2/7
✗ Branch 0 not taken.
✓ Branch 1 taken 324 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 108 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
324 if (!validatedefinename(defname)) asar_throw_error(0, error_type_line, error_id_invalid_define_name);
586 324 }
587 else
588 {
589
2/2
✓ Branch 0 taken 149773 times.
✓ Branch 1 taken 29138 times.
178911 while (is_ualnum(*here)) defname+=*here++;
590 }
591
592
2/2
✓ Branch 0 taken 15665 times.
✓ Branch 1 taken 13797 times.
29462 if (first)
593 {
594 enum {
595 null,
596 append,
597 expand,
598 domath,
599 setifnotset,
600 } mode;
601 if(0);
602
2/2
✓ Branch 0 taken 1500 times.
✓ Branch 1 taken 14165 times.
15665 else if (stribegin(here, " = ")) { here+=3; mode=null; }
603
2/2
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 13859 times.
14165 else if (stribegin(here, " += ")) { here+=4; mode=append; }
604
2/2
✓ Branch 0 taken 162 times.
✓ Branch 1 taken 13697 times.
13859 else if (stribegin(here, " := ")) { here+=4; mode=expand; }
605
2/2
✓ Branch 0 taken 5778 times.
✓ Branch 1 taken 7919 times.
13697 else if (stribegin(here, " #= ")) { here+=4; mode=domath; }
606
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7907 times.
7919 else if (stribegin(here, " ?= ")) { here+=4; mode=setifnotset; }
607 7907 else goto notdefineset;
608 3882 string val;
609
2/2
✓ Branch 0 taken 507 times.
✓ Branch 1 taken 7251 times.
7758 if (*here=='"')
610 {
611 507 here++;
612 while (true)
613 {
614
2/2
✓ Branch 0 taken 2052 times.
✓ Branch 1 taken 525 times.
2577 if (*here=='"')
615 {
616
3/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 507 times.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
525 if (!here[1] || here[1]==' ') break;
617
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 else if (here[1]=='"') here++;
618 else asar_throw_error(0, error_type_line, error_id_broken_define_declaration);
619 }
620 2070 val+=*here++;
621 }
622 507 here++;
623 }
624 else
625 {
626
3/4
✓ Branch 0 taken 49863 times.
✓ Branch 1 taken 7251 times.
✓ Branch 2 taken 49863 times.
✗ Branch 3 not taken.
57114 while (*here && *here!=' ') val+=*here++;
627 }
628 //if (strqchr(val.data(), ';')) *strqchr(val.data(), ';')=0;
629
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 7758 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
7758 if (*here && !stribegin(here, " : ")) asar_throw_error(0, error_type_line, error_id_broken_define_declaration);
630 // RPG Hacker: Is it really a good idea to normalize
631 // the content of defines? That kinda violates their
632 // functionality as a string replacement mechanism.
633
1/2
✓ Branch 0 taken 7758 times.
✗ Branch 1 not taken.
7758 val.qnormalize();
634
635 // RPG Hacker: throw an error if we're trying to overwrite built-in defines.
636
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 7740 times.
7758 if (builtindefines.exists(defname))
637 {
638
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 asar_throw_error(0, error_type_line, error_id_overriding_builtin_define, defname.data());
639 }
640
641
5/5
✓ Branch 0 taken 1482 times.
✓ Branch 1 taken 306 times.
✓ Branch 2 taken 162 times.
✓ Branch 3 taken 5778 times.
✓ Branch 4 taken 12 times.
7740 switch (mode)
642 {
643 case null:
644 {
645
1/2
✓ Branch 0 taken 1482 times.
✗ Branch 1 not taken.
1482 defines.create(defname) = val;
646 break;
647 }
648 case append:
649 {
650
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 306 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
306 if (!defines.exists(defname)) asar_throw_error(0, error_type_line, error_id_define_not_found, defname.data());
651 306 string oldval = defines.find(defname);
652
1/2
✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
306 val=oldval+val;
653
1/2
✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
306 defines.create(defname) = val;
654 break;
655 306 }
656 81 case expand:
657 {
658 81 string newval;
659
1/2
✓ Branch 0 taken 162 times.
✗ Branch 1 not taken.
162 resolvedefines(newval, val);
660
1/2
✓ Branch 0 taken 162 times.
✗ Branch 1 not taken.
162 defines.create(defname) = newval;
661 break;
662 162 }
663 2889 case domath:
664 {
665 2889 string newval;
666
1/2
✓ Branch 0 taken 5778 times.
✗ Branch 1 not taken.
5778 resolvedefines(newval, val);
667
1/2
✓ Branch 0 taken 5778 times.
✗ Branch 1 not taken.
5778 double num= getnumdouble(newval);
668
5/6
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 5742 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 18 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 18 times.
5778 if (foundlabel && !foundlabel_static) asar_throw_error(0, error_type_line, error_id_define_label_math);
669
1/2
✓ Branch 0 taken 5760 times.
✗ Branch 1 not taken.
5760 defines.create(defname) = ftostr(num);
670 break;
671 5778 }
672 case setifnotset:
673 {
674
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 if (!defines.exists(defname)) defines.create(defname) = val;
675 break;
676 }
677 }
678 7758 }
679 else
680 {
681 13797 notdefineset:
682
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 21611 times.
21704 if (!defname) out+="!";
683 else
684 {
685
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 21611 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
21611 if (!defines.exists(defname)) asar_throw_error(0, error_type_line, error_id_define_not_found, defname.data());
686 else {
687 21611 string thisone = defines.find(defname);
688
1/2
✓ Branch 0 taken 21611 times.
✗ Branch 1 not taken.
21611 resolvedefines(out, thisone);
689 21611 }
690 }
691 }
692 33422 }
693 179034 else out+=*here++;
694 }
695
4/6
✓ Branch 0 taken 31070 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 31052 times.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
31070 if (!confirmquotes(out)) { asar_throw_error(0, error_type_null, error_id_mismatched_quotes); out = ""; }
696 177592 }
697
698 bool moreonline;
699 bool asarverallowed = false;
700
701 144765 void assembleline(const char * fname, int linenum, const char * line, int& single_line_for_tracker)
702 {
703 65712 recurseblock rec;
704 144765 bool moreonlinetmp=moreonline;
705 // randomdude999: redundant, assemblefile already converted the path to absolute
706 //string absolutepath = filesystem->create_absolute_path("", fname);
707 65712 string absolutepath = fname;
708 144765 single_line_for_tracker = 1;
709 try
710 {
711 65712 string out=line;
712
1/2
✓ Branch 0 taken 144765 times.
✗ Branch 1 not taken.
144765 out.qreplace(": :", ": :");
713
1/2
✓ Branch 0 taken 144765 times.
✗ Branch 1 not taken.
144765 autoptr<char**> blocks=qsplitstr(out.temp_raw(), " : ");
714 144765 moreonline=true;
715
2/2
✓ Branch 0 taken 149994 times.
✓ Branch 1 taken 128247 times.
278241 for (int block=0;moreonline;block++)
716 {
717 149994 moreonline=(blocks[block+1] != nullptr);
718 try
719 {
720 149994 string stripped_block = strip_whitespace(blocks[block]);
721
722 68349 callstack_push cs_push(callstack_entry_type::BLOCK, stripped_block);
723
724
2/2
✓ Branch 0 taken 132588 times.
✓ Branch 1 taken 17406 times.
149994 assembleblock(stripped_block, single_line_for_tracker);
725
2/2
✓ Branch 0 taken 132576 times.
✓ Branch 1 taken 12 times.
132588 checkbankcross();
726 167412 }
727
2/2
✓ Branch 0 taken 16518 times.
✓ Branch 1 taken 900 times.
17418 catch (errblock&) {}
728
2/2
✓ Branch 0 taken 87585 times.
✓ Branch 1 taken 45891 times.
133476 if (blocks[block][0]!='\0') asarverallowed=false;
729
2/2
✓ Branch 0 taken 127257 times.
✓ Branch 1 taken 6219 times.
133476 if(single_line_for_tracker == 1) single_line_for_tracker = 0;
730 }
731 161283 }
732
1/2
✓ Branch 0 taken 16518 times.
✗ Branch 1 not taken.
16518 catch (errline&) {}
733 128247 moreonline=moreonlinetmp;
734 161283 }
735
736 int incsrcdepth=0;
737
738 // Returns true if a file is protected via
739 // an "includeonce".
740 19170 bool file_included_once(const char* file)
741 {
742
2/2
✓ Branch 0 taken 720 times.
✓ Branch 1 taken 19044 times.
19764 for (int i = 0; i < includeonce.count; ++i)
743 {
744
2/2
✓ Branch 0 taken 594 times.
✓ Branch 1 taken 126 times.
720 if (includeonce[i] == file)
745 {
746 return true;
747 }
748 }
749
750 return false;
751 }
752
753 autoarray<string> macro_defs;
754 int in_macro_def=0;
755
756 19080 void assemblefile(const char * filename)
757 {
758 19080 incsrcdepth++;
759 19080 string absolutepath = filesystem->create_absolute_path(get_current_file_name(), filename);
760
761
4/4
✓ Branch 0 taken 18996 times.
✓ Branch 1 taken 84 times.
✓ Branch 2 taken 1868 times.
✓ Branch 3 taken 42 times.
19080 if (file_included_once(absolutepath))
762 {
763 return;
764 }
765
766 2766 callstack_push cs_push(callstack_entry_type::FILE, absolutepath);
767
768 sourcefile file;
769 18954 file.contents = nullptr;
770 18954 file.numlines = 0;
771
2/2
✓ Branch 0 taken 862 times.
✓ Branch 1 taken 18092 times.
18954 int startif=numif;
772
2/2
✓ Branch 0 taken 862 times.
✓ Branch 1 taken 18092 times.
18954 if (!filecontents.exists(absolutepath))
773 {
774
2/2
✓ Branch 0 taken 856 times.
✓ Branch 1 taken 6 times.
862 char * temp = readfile(absolutepath, "");
775
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 838 times.
856 if (!temp)
776 {
777
3/6
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
18 asar_throw_error(0, error_type_null, vfile_error_to_error_id(asar_get_last_io_error()), filename);
778
779 return;
780 }
781
1/2
✓ Branch 0 taken 838 times.
✗ Branch 1 not taken.
838 sourcefile& newfile = filecontents.create(absolutepath);
782
1/2
✓ Branch 0 taken 838 times.
✗ Branch 1 not taken.
838 newfile.contents =split(temp, '\n');
783 838 newfile.data = temp;
784
2/2
✓ Branch 0 taken 32803 times.
✓ Branch 1 taken 838 times.
33641 for (int i=0;newfile.contents[i];i++)
785 {
786 32803 newfile.numlines++;
787 char * line= newfile.contents[i];
788 32803 char * comment = strqchr(line, ';');
789
2/2
✓ Branch 0 taken 8658 times.
✓ Branch 1 taken 24145 times.
32803 if(comment) *comment = 0;
790
4/6
✓ Branch 0 taken 32803 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 32791 times.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
32809 if (!confirmquotes(line)) { callstack_push cs_push(callstack_entry_type::LINE, line, i); asar_throw_error(0, error_type_null, error_id_mismatched_quotes); line[0] = '\0'; }
791 32803 newfile.contents[i] = strip_whitespace(line);
792 }
793
2/2
✓ Branch 0 taken 32803 times.
✓ Branch 1 taken 838 times.
33641 for(int i=0;newfile.contents[i];i++)
794 {
795 char* line = newfile.contents[i];
796
2/2
✓ Branch 0 taken 12140 times.
✓ Branch 1 taken 20663 times.
32803 if(!*line) continue;
797
3/4
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 20663 times.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
20693 for (int j=1;line[strlen(line) - 1] == ',' && newfile.contents[i+j];j++)
798 {
799 // not using strcat because the source and dest overlap here
800 char* otherline = newfile.contents[i+j];
801 30 char* line_end = line + strlen(line);
802
2/2
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 30 times.
414 while(*otherline) *line_end++ = *otherline++;
803 30 *line_end = '\0';
804 static char nullstr[]="";
805 30 newfile.contents[i+j]=nullstr;
806 }
807 }
808 838 file = newfile;
809 } else { // filecontents.exists(absolutepath)
810 18092 file = filecontents.find(absolutepath);
811 }
812 18930 asarverallowed=true;
813
3/4
✓ Branch 0 taken 133995 times.
✓ Branch 1 taken 2406 times.
✓ Branch 2 taken 133995 times.
✗ Branch 3 not taken.
136401 for (int i=0;file.contents[i] && i<file.numlines;i++)
814 {
815 60327 string connectedline;
816 133995 int skiplines = getconnectedlines<char**>(file.contents, i, connectedline);
817
818
2/2
✓ Branch 0 taken 117471 times.
✓ Branch 1 taken 16524 times.
133995 bool was_loop_end = do_line_logic(connectedline, absolutepath, i);
819 117471 i += skiplines;
820
821 // if a loop ended on this line, should it run again?
822
4/4
✓ Branch 0 taken 6306 times.
✓ Branch 1 taken 111165 times.
✓ Branch 2 taken 2310 times.
✓ Branch 3 taken 3996 times.
117471 if (was_loop_end && whilestatus[numif].cond)
823 3996 i = whilestatus[numif].startline - 1;
824 133995 }
825
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 2406 times.
2442 while (in_macro_def > 0)
826 {
827
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 asar_throw_error(0, error_type_null, error_id_unclosed_macro, macro_defs[in_macro_def-1].data());
828
5/6
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
36 if (!pass && in_macro_def == 1) endmacro(false);
829 36 in_macro_def--;
830 36 macro_defs.remove(in_macro_def);
831 }
832
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2406 times.
2406 if (numif!=startif)
833 {
834 numif=startif;
835 numtrue=startif;
836 asar_throw_error(0, error_type_null, error_id_unclosed_if);
837 }
838 2406 incsrcdepth--;
839 35610 }
840
841 // RPG Hacker: At some point, this should probably be merged
842 // into assembleline(), since the two names just cause
843 // confusion otherwise.
844 // return value is "did a loop end on this line"
845 155067 bool do_line_logic(const char* line, const char* filename, int lineno)
846 {
847 155067 int prevnumif = numif;
848 155067 int single_line_for_tracker = 1;
849 try
850 {
851 70863 string current_line;
852
6/6
✓ Branch 0 taken 9558 times.
✓ Branch 1 taken 145509 times.
✓ Branch 2 taken 8100 times.
✓ Branch 3 taken 1458 times.
✓ Branch 4 taken 504 times.
✓ Branch 5 taken 7596 times.
155067 if (numif==numtrue || (numtrue+1==numif && stribegin(line, "elseif ")))
853 {
854 66336 callstack_push cs_push(callstack_entry_type::LINE, line, lineno);
855
2/2
✓ Branch 0 taken 145707 times.
✓ Branch 1 taken 306 times.
146013 string tmp=replace_macro_args(line);
856
1/2
✓ Branch 0 taken 145707 times.
✗ Branch 1 not taken.
145707 tmp.qnormalize();
857
2/2
✓ Branch 0 taken 145611 times.
✓ Branch 1 taken 96 times.
145707 resolvedefines(current_line, tmp);
858 146109 }
859 else current_line=line;
860
861 70662 callstack_push cs_push(callstack_entry_type::LINE, current_line, lineno);
862
863
4/4
✓ Branch 0 taken 1656 times.
✓ Branch 1 taken 153009 times.
✓ Branch 2 taken 108 times.
✓ Branch 3 taken 1548 times.
154665 if (stribegin(current_line, "macro ") && numif==numtrue)
864 {
865 // RPG Hacker: Slight redundancy here with code that is
866 // also in startmacro(). Could improve this for Asar 2.0.
867 1548 string macro_name = current_line.data()+6;
868 1548 char * startpar=strqchr(macro_name.data(), '(');
869
1/2
✓ Branch 0 taken 1548 times.
✗ Branch 1 not taken.
1548 if (startpar) *startpar=0;
870 1548 macro_defs.append(macro_name);
871
872 // RPG Hacker: I think it would make more logical sense
873 // to have this ++ after the if, but hat breaks compatibility
874 // with at least one test, and it generally leads to more
875 // errors being output after a broken macro declaration.
876 1548 in_macro_def++;
877
2/2
✓ Branch 0 taken 516 times.
✓ Branch 1 taken 1032 times.
1548 if (!pass)
878 {
879
4/4
✓ Branch 0 taken 420 times.
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 414 times.
✓ Branch 3 taken 6 times.
516 if (in_macro_def == 1) startmacro(current_line.data()+6);
880
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
96 else tomacro(current_line);
881 }
882 1548 }
883
6/6
✓ Branch 0 taken 84039 times.
✓ Branch 1 taken 69078 times.
✓ Branch 2 taken 864 times.
✓ Branch 3 taken 83175 times.
✓ Branch 4 taken 54 times.
✓ Branch 5 taken 756 times.
153117 else if (!stricmp(current_line, "endmacro") && numif==numtrue)
884 {
885
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1512 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1512 if (in_macro_def == 0) asar_throw_error(0, error_type_line, error_id_misplaced_endmacro);
886 else
887 {
888 1512 in_macro_def--;
889 1512 macro_defs.remove(in_macro_def);
890
2/2
✓ Branch 0 taken 504 times.
✓ Branch 1 taken 1008 times.
1512 if (!pass)
891 {
892
3/4
✓ Branch 0 taken 414 times.
✓ Branch 1 taken 90 times.
✓ Branch 2 taken 414 times.
✗ Branch 3 not taken.
504 if (in_macro_def == 0) endmacro(true);
893
1/2
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
90 else tomacro(current_line);
894 }
895 }
896 }
897
2/2
✓ Branch 0 taken 6840 times.
✓ Branch 1 taken 144765 times.
151605 else if (in_macro_def > 0)
898 {
899
3/4
✓ Branch 0 taken 2280 times.
✓ Branch 1 taken 4560 times.
✓ Branch 2 taken 2280 times.
✗ Branch 3 not taken.
6840 if (!pass) tomacro(current_line);
900 }
901 else
902 {
903
2/2
✓ Branch 0 taken 128247 times.
✓ Branch 1 taken 16518 times.
144765 assembleline(filename, lineno, current_line, single_line_for_tracker);
904 }
905 171591 }
906
2/2
✓ Branch 0 taken 16524 times.
✓ Branch 1 taken 402 times.
16926 catch (errline&) {}
907
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 121863 times.
122043 return (numif != prevnumif || single_line_for_tracker == 3)
908
6/6
✓ Branch 0 taken 122043 times.
✓ Branch 1 taken 16500 times.
✓ Branch 2 taken 9054 times.
✓ Branch 3 taken 7626 times.
✓ Branch 4 taken 6812 times.
✓ Branch 5 taken 2242 times.
293766 && (whilestatus[numif].iswhile || whilestatus[numif].is_for);
909 }
910
911
912 473 void parse_std_includes(const char* textfile, autoarray<string>& outarray)
913 {
914 473 char* content = readfilenative(textfile);
915
916
1/2
✓ Branch 0 taken 473 times.
✗ Branch 1 not taken.
473 if (content != nullptr)
917 {
918 char* pos = content;
919
920
2/2
✓ Branch 0 taken 948 times.
✓ Branch 1 taken 473 times.
1421 while (pos[0] != '\0')
921 {
922 476 string stdinclude;
923
924 do
925 {
926
3/4
✓ Branch 0 taken 18438 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17963 times.
✓ Branch 3 taken 475 times.
18438 if (pos[0] != '\r' && pos[0] != '\n')
927 {
928 17963 stdinclude += pos[0];
929 }
930 18438 pos++;
931
4/4
✓ Branch 0 taken 17965 times.
✓ Branch 1 taken 473 times.
✓ Branch 2 taken 17490 times.
✓ Branch 3 taken 475 times.
18438 } while (pos[0] != '\0' && pos[0] != '\n');
932
933 948 strip_whitespace(stdinclude);
934
935
2/2
✓ Branch 0 taken 475 times.
✓ Branch 1 taken 473 times.
948 if (stdinclude != "")
936 {
937
3/4
✓ Branch 0 taken 475 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 474 times.
475 if (!path_is_absolute(stdinclude))
938 {
939
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
2 stdinclude = dir(textfile) + stdinclude;
940 }
941
1/2
✓ Branch 0 taken 475 times.
✗ Branch 1 not taken.
475 outarray.append(normalize_path(stdinclude));
942 }
943 948 }
944
945 473 free(content);
946 }
947 473 }
948
949 722 void parse_std_defines(const char* textfile)
950 {
951
952 // RPG Hacker: add built-in defines.
953 // (They're not really standard defines, but I was lazy and this was
954 // one convenient place for doing it).
955 722 builtindefines.create("assembler") = "asar";
956
2/3
✓ Branch 0 taken 469 times.
✓ Branch 1 taken 253 times.
✗ Branch 2 not taken.
722 builtindefines.create("assembler_ver") = dec(get_version_int());
957
1/2
✓ Branch 0 taken 722 times.
✗ Branch 1 not taken.
722 builtindefines.create("assembler_time") = dec(time(nullptr));
958
959
2/2
✓ Branch 0 taken 473 times.
✓ Branch 1 taken 249 times.
722 if(textfile == nullptr) return;
960
961 473 char* content = readfilenative(textfile);
962
963
1/2
✓ Branch 0 taken 473 times.
✗ Branch 1 not taken.
473 if (content != nullptr)
964 {
965 char* pos = content;
966
2/2
✓ Branch 0 taken 2837 times.
✓ Branch 1 taken 473 times.
3310 while (*pos != 0) {
967 1421 string define_name;
968 1421 string define_val;
969
970
4/4
✓ Branch 0 taken 28347 times.
✓ Branch 1 taken 1890 times.
✓ Branch 2 taken 27400 times.
✓ Branch 3 taken 947 times.
30237 while (*pos != '=' && *pos != '\n') {
971 27400 define_name += *pos;
972 27400 pos++;
973 }
974
2/2
✓ Branch 0 taken 1890 times.
✓ Branch 1 taken 947 times.
2837 if (*pos != 0 && *pos != '\n') pos++; // skip =
975
3/4
✓ Branch 0 taken 12762 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9925 times.
✓ Branch 3 taken 2837 times.
12762 while (*pos != 0 && *pos != '\n') {
976 9925 define_val += *pos;
977 9925 pos++;
978 }
979
1/2
✓ Branch 0 taken 2837 times.
✗ Branch 1 not taken.
2837 if (*pos != 0)
980 2837 pos++; // skip \n
981 // clean define_name
982 2837 strip_whitespace(define_name);
983 2837 define_name.strip_prefix('!'); // remove leading ! if present
984
985
2/2
✓ Branch 0 taken 473 times.
✓ Branch 1 taken 2364 times.
2837 if (define_name == "")
986 {
987
1/2
✓ Branch 0 taken 473 times.
✗ Branch 1 not taken.
473 if (define_val == "")
988 {
989 473 continue;
990 }
991
992 asar_throw_error(pass, error_type_null, error_id_stddefines_no_identifier);
993 }
994
995
2/7
✗ Branch 0 not taken.
✓ Branch 1 taken 2364 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 594 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
2364 if (!validatedefinename(define_name)) asar_throw_error(pass, error_type_null, error_id_cmdl_define_invalid, "stddefines.txt", define_name.data());
996
997 // clean define_val
998 const char* defval = define_val.data();
999 1184 string cleaned_defval;
1000
1001
2/2
✓ Branch 0 taken 474 times.
✓ Branch 1 taken 1890 times.
2364 if (*defval == 0) {
1002 // no value
1003
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 474 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
474 if (clidefines.exists(define_name)) asar_throw_error(pass, error_type_null, error_id_cmdl_define_override, "Std define", define_name.data());
1004
1/2
✓ Branch 0 taken 474 times.
✗ Branch 1 not taken.
474 clidefines.create(define_name) = "";
1005 474 continue;
1006 }
1007
1008
3/4
✓ Branch 0 taken 946 times.
✓ Branch 1 taken 1890 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1890 times.
2836 while (*defval == ' ' || *defval == '\t') defval++; // skip whitespace in beginning
1009
2/2
✓ Branch 0 taken 473 times.
✓ Branch 1 taken 1417 times.
1890 if (*defval == '"') {
1010 473 defval++; // skip opening quote
1011
3/4
✓ Branch 0 taken 6141 times.
✓ Branch 1 taken 473 times.
✓ Branch 2 taken 6141 times.
✗ Branch 3 not taken.
6614 while (*defval != '"' && *defval != 0)
1012 6141 cleaned_defval += *defval++;
1013
1014
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 473 times.
473 if (*defval == 0) {
1015 asar_throw_error(pass, error_type_null, error_id_mismatched_quotes);
1016 }
1017 473 defval++; // skip closing quote
1018
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 473 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 473 times.
473 while (*defval == ' ' || *defval == '\t') defval++; // skip whitespace
1019
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 473 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
473 if (*defval != 0 && *defval != '\n')
1020 asar_throw_error(pass, error_type_null, error_id_stddefine_after_closing_quote);
1021
1022
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 473 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
473 if (clidefines.exists(define_name)) asar_throw_error(pass, error_type_null, error_id_cmdl_define_override, "Std define", define_name.data());
1023
1/2
✓ Branch 0 taken 473 times.
✗ Branch 1 not taken.
473 clidefines.create(define_name) = cleaned_defval;
1024 473 continue;
1025 }
1026 else
1027 {
1028 // slightly hacky way to remove trailing whitespace
1029 708 const char* defval_end = strchr(defval, '\n'); // slightly hacky way to get end of string or newline
1030
1/2
✓ Branch 0 taken 1417 times.
✗ Branch 1 not taken.
1417 if (!defval_end) defval_end = strchr(defval, 0);
1031 1417 defval_end--;
1032
3/4
✓ Branch 0 taken 475 times.
✓ Branch 1 taken 1417 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1417 times.
1892 while (*defval_end == ' ' || *defval_end == '\t') defval_end--;
1033
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1417 times.
1417 cleaned_defval = string(defval, (int)(defval_end - defval + 1));
1034
1035
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1417 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1417 if (clidefines.exists(define_name)) asar_throw_error(pass, error_type_null, error_id_cmdl_define_override, "Std define", define_name.data());
1036
1/2
✓ Branch 0 taken 1417 times.
✗ Branch 1 not taken.
1417 clidefines.create(define_name) = cleaned_defval;
1037 1417 continue;
1038 1417 }
1039
1040 2837 }
1041 473 free(content);
1042 }
1043 }
1044
1045 bool checksum_fix_enabled = true;
1046 bool force_checksum_fix = false;
1047
1048 #define cfree(x) free((void*)x)
1049 414 static void clearmacro(const string & key, macrodata* & macro)
1050 {
1051 (void)key;
1052 414 freemacro(macro);
1053 414 }
1054
1055 838 static void clearfile(const string & key, sourcefile& filecontent)
1056 {
1057 (void)key;
1058 838 cfree(filecontent.data);
1059 838 cfree(filecontent.contents);
1060 838 }
1061 #undef cfree
1062
1063
1/2
✓ Branch 0 taken 4526 times.
✗ Branch 1 not taken.
4526 static void adddefine(const string & key, string & value)
1064 {
1065
1/2
✓ Branch 0 taken 4526 times.
✗ Branch 1 not taken.
4526 if (!defines.exists(key)) defines.create(key) = value;
1066 4526 }
1067
1068 static string symbolfile;
1069
1070 322 static void printsymbol_wla(const string& key, snes_label& label)
1071 {
1072 644 string line = hex((label.pos & 0xFF0000)>>16, 2)+":"+hex(label.pos & 0xFFFF, 4)+" "+key+"\n";
1073 322 symbolfile += line;
1074 322 }
1075
1076 static void printsymbol_nocash(const string& key, snes_label& label)
1077 {
1078 string line = hex(label.pos & 0xFFFFFF, 8)+" "+key+"\n";
1079 symbolfile += line;
1080 }
1081
1082 168 string create_symbols_file(string format, uint32_t romCrc){
1083 168 format = lower(format);
1084 symbolfile = "";
1085
1/2
✓ Branch 0 taken 168 times.
✗ Branch 1 not taken.
168 if(format == "wla")
1086 {
1087 symbolfile = "; wla symbolic information file\n";
1088 168 symbolfile += "; generated by asar\n";
1089
1090 168 symbolfile += "\n[labels]\n";
1091 168 labels.each(printsymbol_wla);
1092
1093 168 symbolfile += "\n[source files]\n";
1094 const autoarray<AddressToLineMapping::FileInfo>& addrToLineFileList = addressToLineMapping.getFileList();
1095
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 168 times.
346 for (int i = 0; i < addrToLineFileList.count; ++i)
1096 {
1097 char addrToFileListStr[256];
1098 178 snprintf(addrToFileListStr, 256, "%.4x %.8x %s\n",
1099 i,
1100 178 addrToLineFileList[i].fileCrc,
1101 addrToLineFileList[i].filename.data()
1102 );
1103 178 symbolfile += addrToFileListStr;
1104 }
1105
1106 168 symbolfile += "\n[rom checksum]\n";
1107 {
1108 char romCrcStr[32];
1109 168 snprintf(romCrcStr, 32, "%.8x\n",
1110 romCrc
1111 );
1112 168 symbolfile += romCrcStr;
1113 }
1114
1115 168 symbolfile += "\n[addr-to-line mapping]\n";
1116 const autoarray<AddressToLineMapping::AddrToLineInfo>& addrToLineInfo = addressToLineMapping.getAddrToLineInfo();
1117
2/2
✓ Branch 0 taken 5242 times.
✓ Branch 1 taken 168 times.
5410 for (int i = 0; i < addrToLineInfo.count; ++i)
1118 {
1119 char addrToLineStr[32];
1120 5242 snprintf(addrToLineStr, 32, "%.2x:%.4x %.4x:%.8x\n",
1121 5242 (addrToLineInfo[i].addr & 0xFF0000) >> 16,
1122 5242 addrToLineInfo[i].addr & 0xFFFF,
1123 5242 addrToLineInfo[i].fileIdx & 0xFFFF,
1124 5242 addrToLineInfo[i].line & 0xFFFFFFFF
1125 );
1126 5242 symbolfile += addrToLineStr;
1127 }
1128
1129 }
1130 else if (format == "nocash")
1131 {
1132 symbolfile = ";no$sns symbolic information file\n";
1133 symbolfile += ";generated by asar\n";
1134 symbolfile += "\n";
1135 labels.each(printsymbol_nocash);
1136 }
1137 168 return symbolfile;
1138 }
1139
1140
1141 6 bool in_top_level_file()
1142 {
1143 int num_files = 0;
1144
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 6 times.
30 for (int i = callstack.count-1; i >= 0; --i)
1145 {
1146
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 18 times.
24 if (callstack[i].type == callstack_entry_type::FILE)
1147 {
1148 6 num_files++;
1149
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (num_files > 1) break;
1150 }
1151 }
1152 6 return (num_files <= 1);
1153 }
1154
1155 36201 const char* get_current_file_name()
1156 {
1157
2/2
✓ Branch 0 taken 135240 times.
✓ Branch 1 taken 2107 times.
137347 for (int i = callstack.count-1; i >= 0; --i)
1158 {
1159
2/2
✓ Branch 0 taken 34094 times.
✓ Branch 1 taken 101146 times.
135240 if (callstack[i].type == callstack_entry_type::FILE)
1160 34094 return callstack[i].content.raw();
1161 }
1162 return nullptr;
1163 }
1164
1165 26229 int get_current_line()
1166 {
1167
2/2
✓ Branch 0 taken 77584 times.
✓ Branch 1 taken 21 times.
77605 for (int i = callstack.count-1; i >= 0; --i)
1168 {
1169
2/2
✓ Branch 0 taken 26208 times.
✓ Branch 1 taken 51376 times.
103792 if (callstack[i].type == callstack_entry_type::LINE) return callstack[i].lineno;
1170 }
1171 return -1;
1172 }
1173
1174 694 const char* get_current_block()
1175 {
1176
2/2
✓ Branch 0 taken 699 times.
✓ Branch 1 taken 31 times.
730 for (int i = callstack.count-1; i >= 0; --i)
1177 {
1178
4/4
✓ Branch 0 taken 537 times.
✓ Branch 1 taken 162 times.
✓ Branch 2 taken 501 times.
✓ Branch 3 taken 36 times.
1899 if (callstack[i].type == callstack_entry_type::LINE || callstack[i].type == callstack_entry_type::BLOCK) return callstack[i].content.raw();
1179 }
1180 return nullptr;
1181 }
1182
1183
1184 727 void reseteverything()
1185 {
1186 374 string str;
1187 727 labels.reset();
1188 727 defines.reset();
1189
1/2
✓ Branch 0 taken 727 times.
✗ Branch 1 not taken.
727 builtindefines.each(adddefine);
1190
1/2
✓ Branch 0 taken 727 times.
✗ Branch 1 not taken.
727 clidefines.each(adddefine);
1191 727 structs.reset();
1192
1193
1/2
✓ Branch 0 taken 727 times.
✗ Branch 1 not taken.
727 macros.each(clearmacro);
1194 727 macros.reset();
1195
1196
1/2
✓ Branch 0 taken 727 times.
✗ Branch 1 not taken.
727 filecontents.each(clearfile);
1197 727 filecontents.reset();
1198
1199 727 writtenblocks.reset();
1200
1201 727 optimizeforbank=-1;
1202 727 optimize_dp = optimize_dp_flag::NONE;
1203 727 dp_base = 0;
1204 727 optimize_address = optimize_address_flag::DEFAULT;
1205
1206
1/2
✓ Branch 0 taken 727 times.
✗ Branch 1 not taken.
727 closecachedfiles();
1207
1208 727 incsrcdepth=0;
1209 727 label_counter = 0;
1210 727 errored = false;
1211 727 checksum_fix_enabled = true;
1212 727 force_checksum_fix = false;
1213
1214 727 in_macro_def = 0;
1215
1216 #ifndef ASAR_SHARED
1217 236 free(const_cast<unsigned char*>(romdata_r));
1218 #endif
1219
1220 727 callstack.reset();
1221 727 simple_callstacks = true;
1222 #undef free
1223 727 }
1224