asar coverage - build #92


src/asar/
File: src/asar/main.cpp
Date: 2024-01-19 17:27:19
Lines:
556/580
95.9%
Functions:
37/38
97.4%
Branches:
517/716
72.2%

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 424 int get_version_int()
47 {
48 424 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 17427 string shorten_to_relative_path(const char* base_path, const char* target_path)
103 {
104
2/2
✓ Branch 0 taken 17421 times.
✓ Branch 1 taken 6 times.
17427 if (stribegin(target_path, base_path)) target_path += strlen(base_path);
105 17427 return target_path;
106 }
107
108 17427 string get_top_level_directory()
109 {
110 1965 string top_level_file_dir;
111
1/2
✓ Branch 0 taken 17427 times.
✗ Branch 1 not taken.
17427 for (int i = 0; i < callstack.count; ++i)
112 {
113
1/2
✓ Branch 0 taken 17427 times.
✗ Branch 1 not taken.
17427 if (callstack[i].type == callstack_entry_type::FILE)
114 {
115
1/2
✓ Branch 0 taken 17427 times.
✗ Branch 1 not taken.
17427 top_level_file_dir = dir(callstack[i].content);
116 17427 break;
117 }
118 }
119 17427 return top_level_file_dir;
120 }
121
122 17343 string generate_call_details_string(const char* current_block, const char* current_call, int indentation, bool add_lines)
123 {
124 1923 string e;
125
2/2
✓ Branch 0 taken 17295 times.
✓ Branch 1 taken 48 times.
17343 if (current_block != nullptr || current_call != nullptr)
126 {
127 1899 string indent;
128
2/2
✓ Branch 0 taken 5548 times.
✓ Branch 1 taken 11747 times.
17295 if (add_lines) indent += "|";
129
2/2
✓ Branch 0 taken 69180 times.
✓ Branch 1 taken 17295 times.
86475 for (; indentation > 0; --indentation) indent += " ";
130
131
2/2
✓ Branch 0 taken 17169 times.
✓ Branch 1 taken 126 times.
34464 if (current_block != nullptr) e += STR "\n"+indent+"in block: ["+current_block+"]";
132
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 17169 times.
17421 if (current_call != nullptr) e += STR "\n"+indent+"in macro call: [%"+current_call+"]";
133 17295 }
134 17343 return e;
135 }
136
137 17427 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 1828 times.
✗ Branch 1 not taken.
17427 return shorten_to_relative_path(get_top_level_directory(), current_file);
145 }
146
147 6331 string generate_filename_and_line(const char* current_file, int current_line_no)
148 {
149 7248 return STR current_file
150
6/6
✓ Branch 0 taken 6295 times.
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 6295 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 6295 times.
✓ Branch 5 taken 36 times.
12662 + (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 700 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 2340 times.
✓ Branch 1 taken 1 times.
2341 for (int i = callstack.count-1; i >= 0 ; --i)
184 {
185
3/5
✓ Branch 0 taken 699 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 663 times.
✓ Branch 3 taken 978 times.
✗ Branch 4 not taken.
2340 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 677 times.
699 if (exclude_block) current_block = nullptr;
190
1/2
✓ Branch 0 taken 235 times.
✗ Branch 1 not taken.
699 *location = generate_filename_and_line(get_pretty_filename(current_file), current_line_no);
191 699 *details = generate_call_details_string(current_block, current_call, 4, false);
192 699 return;
193 case callstack_entry_type::MACRO_CALL:
194 if (current_call == nullptr) current_call = callstack[i].content;
195 break;
196 663 case callstack_entry_type::LINE:
197
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 495 times.
663 if (current_block == nullptr && current_call == nullptr) current_block = callstack[i].content;
198
1/2
✓ Branch 0 taken 663 times.
✗ Branch 1 not taken.
1326 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 700 void get_full_printable_callstack(autoarray<printable_callstack_entry>* out, int indentation, bool add_lines)
210 {
211 700 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 69042 times.
✓ Branch 1 taken 700 times.
69742 for (int i = 0; i < callstack.count; ++i)
217 {
218
4/5
✓ Branch 0 taken 17343 times.
✓ Branch 1 taken 126 times.
✓ Branch 2 taken 17307 times.
✓ Branch 3 taken 34266 times.
✗ Branch 4 not taken.
69042 switch (callstack[i].type)
219 {
220 17343 case callstack_entry_type::FILE:
221
2/2
✓ Branch 0 taken 16644 times.
✓ Branch 1 taken 699 times.
17343 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 17343 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 17307 current_line_no = callstack[i].lineno;
236 current_block = callstack[i].content;
237 17307 break;
238 case callstack_entry_type::BLOCK:
239 current_block = callstack[i].content;
240 34266 break;
241 }
242 }
243 700 }
244
245 232 string get_full_callstack()
246 {
247 116 autoarray<printable_callstack_entry> printable_stack;
248
1/2
✓ Branch 0 taken 232 times.
✗ Branch 1 not taken.
232 get_full_printable_callstack(&printable_stack, 12, true);
249
250 116 string e;
251
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 182 times.
232 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 232 return e;
260 232 }
261
262 // RPG Hacker: This function essetially replicates classic Asar behavior
263 // of only printing a single macro call below the current level.
264 468 string get_simple_callstack()
265 {
266 int i;
267 const char* current_call = nullptr;
268
2/2
✓ Branch 0 taken 45696 times.
✓ Branch 1 taken 384 times.
46080 for (i = callstack.count-1; i >= 0 ; --i)
269 {
270
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 45612 times.
45696 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 384 times.
468 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 236 string e;
311
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 384 times.
468 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 468 return e;
317 }
318
319 700 string get_callstack()
320 {
321
2/2
✓ Branch 0 taken 468 times.
✓ Branch 1 taken 232 times.
700 if (simple_callstacks)
322 468 return get_simple_callstack();
323 else
324 232 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 5104 bool validatedefinename(const char * name)
514 {
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5104 times.
5104 if (!name[0]) return false;
516
2/2
✓ Branch 0 taken 53048 times.
✓ Branch 1 taken 5104 times.
58152 for (int i = 0;name[i];i++)
517 {
518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53048 times.
53048 if (!is_ualnum(name[i])) return false;
519 }
520
521 return true;
522 }
523
524 177976 void resolvedefines(string& out, const char * start)
525 {
526 82331 recurseblock rec;
527 const char * here=start;
528
2/2
✓ Branch 0 taken 146810 times.
✓ Branch 1 taken 31160 times.
177970 if (!strchr(here, '!'))
529 {
530 146810 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 177970 }
697
698 bool moreonline;
699 bool asarverallowed = false;
700
701 145143 void assembleline(const char * fname, int linenum, const char * line, int& single_line_for_tracker)
702 {
703 65901 recurseblock rec;
704 145143 bool moreonlinetmp=moreonline;
705 // randomdude999: redundant, assemblefile already converted the path to absolute
706 //string absolutepath = filesystem->create_absolute_path("", fname);
707 65901 string absolutepath = fname;
708 145143 single_line_for_tracker = 1;
709 try
710 {
711 65901 string out=line;
712
1/2
✓ Branch 0 taken 145143 times.
✗ Branch 1 not taken.
145143 out.qreplace(": :", ": :");
713
1/2
✓ Branch 0 taken 145143 times.
✗ Branch 1 not taken.
145143 autoptr<char**> blocks=qsplitstr(out.temp_raw(), " : ");
714 145143 moreonline=true;
715
2/2
✓ Branch 0 taken 150372 times.
✓ Branch 1 taken 128625 times.
278997 for (int block=0;moreonline;block++)
716 {
717 150372 moreonline=(blocks[block+1] != nullptr);
718 try
719 {
720 150372 string stripped_block = strip_whitespace(blocks[block]);
721
722 68538 callstack_push cs_push(callstack_entry_type::BLOCK, stripped_block);
723
724
2/2
✓ Branch 0 taken 132966 times.
✓ Branch 1 taken 17406 times.
150372 assembleblock(stripped_block, single_line_for_tracker);
725
2/2
✓ Branch 0 taken 132954 times.
✓ Branch 1 taken 12 times.
132966 checkbankcross();
726 167790 }
727
2/2
✓ Branch 0 taken 16518 times.
✓ Branch 1 taken 900 times.
17418 catch (errblock&) {}
728
2/2
✓ Branch 0 taken 87639 times.
✓ Branch 1 taken 46215 times.
133854 if (blocks[block][0]!='\0') asarverallowed=false;
729
2/2
✓ Branch 0 taken 127635 times.
✓ Branch 1 taken 6219 times.
133854 if(single_line_for_tracker == 1) single_line_for_tracker = 0;
730 }
731 161661 }
732
1/2
✓ Branch 0 taken 16518 times.
✗ Branch 1 not taken.
16518 catch (errline&) {}
733 128625 moreonline=moreonlinetmp;
734 161661 }
735
736 int incsrcdepth=0;
737
738 // Returns true if a file is protected via
739 // an "includeonce".
740 19188 bool file_included_once(const char* file)
741 {
742
2/2
✓ Branch 0 taken 720 times.
✓ Branch 1 taken 19062 times.
19782 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 19098 void assemblefile(const char * filename)
757 {
758 19098 incsrcdepth++;
759 19098 string absolutepath = filesystem->create_absolute_path(get_current_file_name(), filename);
760
761
4/4
✓ Branch 0 taken 19014 times.
✓ Branch 1 taken 84 times.
✓ Branch 2 taken 1874 times.
✓ Branch 3 taken 42 times.
19098 if (file_included_once(absolutepath))
762 {
763 return;
764 }
765
766 2775 callstack_push cs_push(callstack_entry_type::FILE, absolutepath);
767
768 sourcefile file;
769 18972 file.contents = nullptr;
770 18972 file.numlines = 0;
771
2/2
✓ Branch 0 taken 868 times.
✓ Branch 1 taken 18104 times.
18972 int startif=numif;
772
2/2
✓ Branch 0 taken 868 times.
✓ Branch 1 taken 18104 times.
18972 if (!filecontents.exists(absolutepath))
773 {
774
2/2
✓ Branch 0 taken 862 times.
✓ Branch 1 taken 6 times.
868 char * temp = readfile(absolutepath, "");
775
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 844 times.
862 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 18 return;
780 }
781
1/2
✓ Branch 0 taken 844 times.
✗ Branch 1 not taken.
844 sourcefile& newfile = filecontents.create(absolutepath);
782
1/2
✓ Branch 0 taken 844 times.
✗ Branch 1 not taken.
844 newfile.contents =split(temp, '\n');
783 844 newfile.data = temp;
784 bool in_block_comment = false;
785 int block_comment_start = -1;
786 435 string block_comment_start_line;
787
2/2
✓ Branch 0 taken 32929 times.
✓ Branch 1 taken 844 times.
33773 for (int i=0;newfile.contents[i];i++)
788 {
789 32929 newfile.numlines++;
790 char * line= newfile.contents[i];
791
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 32887 times.
32929 if(in_block_comment) {
792 21 char * end = strstr(line, "]]");
793
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 6 times.
42 if(!end) {
794 36 *line = 0;
795 36 continue;
796 }
797 6 line = end+2;
798 in_block_comment = false;
799 }
800 32893 char * comment = strqchr(line, ';');
801
2/2
✓ Branch 0 taken 8718 times.
✓ Branch 1 taken 24175 times.
32893 if(comment) {
802
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 8706 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
8718 if(comment[1] == '[' && comment[2] == '[') {
803 in_block_comment = true;
804 block_comment_start = i;
805 block_comment_start_line = line;
806 }
807 8718 *comment = 0;
808 }
809
4/6
✓ Branch 0 taken 32893 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 32881 times.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
32899 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'; }
810 32893 newfile.contents[i] = strip_whitespace(line);
811 }
812
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 838 times.
844 if(in_block_comment) {
813 3 callstack_push cs_push(callstack_entry_type::LINE, block_comment_start_line, block_comment_start);
814
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 asar_throw_error(0, error_type_null, error_id_unclosed_block_comment);
815 6 }
816
2/2
✓ Branch 0 taken 32929 times.
✓ Branch 1 taken 844 times.
33773 for(int i=0;newfile.contents[i];i++)
817 {
818 char* line = newfile.contents[i];
819
2/2
✓ Branch 0 taken 12248 times.
✓ Branch 1 taken 20681 times.
32929 if(!*line) continue;
820
3/4
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 20681 times.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
20711 for (int j=1;line[strlen(line) - 1] == ',' && newfile.contents[i+j];j++)
821 {
822 // not using strcat because the source and dest overlap here
823 char* otherline = newfile.contents[i+j];
824 30 char* line_end = line + strlen(line);
825
2/2
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 30 times.
414 while(*otherline) *line_end++ = *otherline++;
826 30 *line_end = '\0';
827 static char nullstr[]="";
828 30 newfile.contents[i+j]=nullstr;
829 }
830 }
831 844 file = newfile;
832 844 } else { // filecontents.exists(absolutepath)
833 18104 file = filecontents.find(absolutepath);
834 }
835 18948 asarverallowed=true;
836
3/4
✓ Branch 0 taken 134373 times.
✓ Branch 1 taken 2424 times.
✓ Branch 2 taken 134373 times.
✗ Branch 3 not taken.
136797 for (int i=0;file.contents[i] && i<file.numlines;i++)
837 {
838 60516 string connectedline;
839 134373 int skiplines = getconnectedlines<char**>(file.contents, i, connectedline);
840
841
2/2
✓ Branch 0 taken 117849 times.
✓ Branch 1 taken 16524 times.
134373 bool was_loop_end = do_line_logic(connectedline, absolutepath, i);
842 117849 i += skiplines;
843
844 // if a loop ended on this line, should it run again?
845
4/4
✓ Branch 0 taken 6306 times.
✓ Branch 1 taken 111543 times.
✓ Branch 2 taken 2310 times.
✓ Branch 3 taken 3996 times.
117849 if (was_loop_end && whilestatus[numif].cond)
846 3996 i = whilestatus[numif].startline - 1;
847 134373 }
848
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 2424 times.
2460 while (in_macro_def > 0)
849 {
850
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());
851
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);
852 36 in_macro_def--;
853 36 macro_defs.remove(in_macro_def);
854 }
855
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2424 times.
2424 if (numif!=startif)
856 {
857 numif=startif;
858 numtrue=startif;
859 asar_throw_error(0, error_type_null, error_id_unclosed_if);
860 }
861 2424 incsrcdepth--;
862 35628 }
863
864 // RPG Hacker: At some point, this should probably be merged
865 // into assembleline(), since the two names just cause
866 // confusion otherwise.
867 // return value is "did a loop end on this line"
868 155445 bool do_line_logic(const char* line, const char* filename, int lineno)
869 {
870 155445 int prevnumif = numif;
871 155445 int single_line_for_tracker = 1;
872 try
873 {
874 71052 string current_line;
875
6/6
✓ Branch 0 taken 9558 times.
✓ Branch 1 taken 145887 times.
✓ Branch 2 taken 8100 times.
✓ Branch 3 taken 1458 times.
✓ Branch 4 taken 504 times.
✓ Branch 5 taken 7596 times.
155445 if (numif==numtrue || (numtrue+1==numif && stribegin(line, "elseif ")))
876 {
877 66525 callstack_push cs_push(callstack_entry_type::LINE, line, lineno);
878
2/2
✓ Branch 0 taken 146085 times.
✓ Branch 1 taken 306 times.
146391 string tmp=replace_macro_args(line);
879
1/2
✓ Branch 0 taken 146085 times.
✗ Branch 1 not taken.
146085 tmp.qnormalize();
880
2/2
✓ Branch 0 taken 145989 times.
✓ Branch 1 taken 96 times.
146085 resolvedefines(current_line, tmp);
881 146487 }
882 else current_line=line;
883
884 70851 callstack_push cs_push(callstack_entry_type::LINE, current_line, lineno);
885
886
4/4
✓ Branch 0 taken 1656 times.
✓ Branch 1 taken 153387 times.
✓ Branch 2 taken 108 times.
✓ Branch 3 taken 1548 times.
155043 if (stribegin(current_line, "macro ") && numif==numtrue)
887 {
888 // RPG Hacker: Slight redundancy here with code that is
889 // also in startmacro(). Could improve this for Asar 2.0.
890 1548 string macro_name = current_line.data()+6;
891 1548 char * startpar=strqchr(macro_name.data(), '(');
892
1/2
✓ Branch 0 taken 1548 times.
✗ Branch 1 not taken.
1548 if (startpar) *startpar=0;
893 1548 macro_defs.append(macro_name);
894
895 // RPG Hacker: I think it would make more logical sense
896 // to have this ++ after the if, but hat breaks compatibility
897 // with at least one test, and it generally leads to more
898 // errors being output after a broken macro declaration.
899 1548 in_macro_def++;
900
2/2
✓ Branch 0 taken 516 times.
✓ Branch 1 taken 1032 times.
1548 if (!pass)
901 {
902
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);
903
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
96 else tomacro(current_line);
904 }
905 1548 }
906
6/6
✓ Branch 0 taken 84228 times.
✓ Branch 1 taken 69267 times.
✓ Branch 2 taken 864 times.
✓ Branch 3 taken 83364 times.
✓ Branch 4 taken 54 times.
✓ Branch 5 taken 756 times.
153495 else if (!stricmp(current_line, "endmacro") && numif==numtrue)
907 {
908
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);
909 else
910 {
911 1512 in_macro_def--;
912 1512 macro_defs.remove(in_macro_def);
913
2/2
✓ Branch 0 taken 504 times.
✓ Branch 1 taken 1008 times.
1512 if (!pass)
914 {
915
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);
916
1/2
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
90 else tomacro(current_line);
917 }
918 }
919 }
920
2/2
✓ Branch 0 taken 6840 times.
✓ Branch 1 taken 145143 times.
151983 else if (in_macro_def > 0)
921 {
922
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);
923 }
924 else
925 {
926
2/2
✓ Branch 0 taken 128625 times.
✓ Branch 1 taken 16518 times.
145143 assembleline(filename, lineno, current_line, single_line_for_tracker);
927 }
928 171969 }
929
2/2
✓ Branch 0 taken 16524 times.
✓ Branch 1 taken 402 times.
16926 catch (errline&) {}
930
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 122241 times.
122421 return (numif != prevnumif || single_line_for_tracker == 3)
931
6/6
✓ Branch 0 taken 122421 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.
294522 && (whilestatus[numif].iswhile || whilestatus[numif].is_for);
932 }
933
934
935 477 void parse_std_includes(const char* textfile, autoarray<string>& outarray)
936 {
937 477 char* content = readfilenative(textfile);
938
939
1/2
✓ Branch 0 taken 477 times.
✗ Branch 1 not taken.
477 if (content != nullptr)
940 {
941 char* pos = content;
942
943
2/2
✓ Branch 0 taken 956 times.
✓ Branch 1 taken 477 times.
1433 while (pos[0] != '\0')
944 {
945 480 string stdinclude;
946
947 do
948 {
949
3/4
✓ Branch 0 taken 18594 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18115 times.
✓ Branch 3 taken 479 times.
18594 if (pos[0] != '\r' && pos[0] != '\n')
950 {
951 18115 stdinclude += pos[0];
952 }
953 18594 pos++;
954
4/4
✓ Branch 0 taken 18117 times.
✓ Branch 1 taken 477 times.
✓ Branch 2 taken 17638 times.
✓ Branch 3 taken 479 times.
18594 } while (pos[0] != '\0' && pos[0] != '\n');
955
956 956 strip_whitespace(stdinclude);
957
958
2/2
✓ Branch 0 taken 479 times.
✓ Branch 1 taken 477 times.
956 if (stdinclude != "")
959 {
960
3/4
✓ Branch 0 taken 479 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 478 times.
479 if (!path_is_absolute(stdinclude))
961 {
962
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
2 stdinclude = dir(textfile) + stdinclude;
963 }
964
1/2
✓ Branch 0 taken 479 times.
✗ Branch 1 not taken.
479 outarray.append(normalize_path(stdinclude));
965 }
966 956 }
967
968 477 free(content);
969 }
970 477 }
971
972 728 void parse_std_defines(const char* textfile)
973 {
974
975 // RPG Hacker: add built-in defines.
976 // (They're not really standard defines, but I was lazy and this was
977 // one convenient place for doing it).
978 728 builtindefines.create("assembler") = "asar";
979
2/3
✓ Branch 0 taken 473 times.
✓ Branch 1 taken 255 times.
✗ Branch 2 not taken.
728 builtindefines.create("assembler_ver") = dec(get_version_int());
980
1/2
✓ Branch 0 taken 728 times.
✗ Branch 1 not taken.
728 builtindefines.create("assembler_time") = dec(time(nullptr));
981
982
2/2
✓ Branch 0 taken 477 times.
✓ Branch 1 taken 251 times.
728 if(textfile == nullptr) return;
983
984 477 char* content = readfilenative(textfile);
985
986
1/2
✓ Branch 0 taken 477 times.
✗ Branch 1 not taken.
477 if (content != nullptr)
987 {
988 char* pos = content;
989
2/2
✓ Branch 0 taken 2861 times.
✓ Branch 1 taken 477 times.
3338 while (*pos != 0) {
990 1433 string define_name;
991 1433 string define_val;
992
993
4/4
✓ Branch 0 taken 28587 times.
✓ Branch 1 taken 1906 times.
✓ Branch 2 taken 27632 times.
✓ Branch 3 taken 955 times.
30493 while (*pos != '=' && *pos != '\n') {
994 27632 define_name += *pos;
995 27632 pos++;
996 }
997
2/2
✓ Branch 0 taken 1906 times.
✓ Branch 1 taken 955 times.
2861 if (*pos != 0 && *pos != '\n') pos++; // skip =
998
3/4
✓ Branch 0 taken 12870 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10009 times.
✓ Branch 3 taken 2861 times.
12870 while (*pos != 0 && *pos != '\n') {
999 10009 define_val += *pos;
1000 10009 pos++;
1001 }
1002
1/2
✓ Branch 0 taken 2861 times.
✗ Branch 1 not taken.
2861 if (*pos != 0)
1003 2861 pos++; // skip \n
1004 // clean define_name
1005 2861 strip_whitespace(define_name);
1006 2861 define_name.strip_prefix('!'); // remove leading ! if present
1007
1008
2/2
✓ Branch 0 taken 477 times.
✓ Branch 1 taken 2384 times.
2861 if (define_name == "")
1009 {
1010
1/2
✓ Branch 0 taken 477 times.
✗ Branch 1 not taken.
477 if (define_val == "")
1011 {
1012 477 continue;
1013 }
1014
1015 asar_throw_error(pass, error_type_null, error_id_stddefines_no_identifier);
1016 }
1017
1018
2/7
✗ Branch 0 not taken.
✓ Branch 1 taken 2384 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 599 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
2384 if (!validatedefinename(define_name)) asar_throw_error(pass, error_type_null, error_id_cmdl_define_invalid, "stddefines.txt", define_name.data());
1019
1020 // clean define_val
1021 const char* defval = define_val.data();
1022 1194 string cleaned_defval;
1023
1024
2/2
✓ Branch 0 taken 478 times.
✓ Branch 1 taken 1906 times.
2384 if (*defval == 0) {
1025 // no value
1026
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 478 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
478 if (clidefines.exists(define_name)) asar_throw_error(pass, error_type_null, error_id_cmdl_define_override, "Std define", define_name.data());
1027
1/2
✓ Branch 0 taken 478 times.
✗ Branch 1 not taken.
478 clidefines.create(define_name) = "";
1028 478 continue;
1029 }
1030
1031
3/4
✓ Branch 0 taken 954 times.
✓ Branch 1 taken 1906 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1906 times.
2860 while (*defval == ' ' || *defval == '\t') defval++; // skip whitespace in beginning
1032
2/2
✓ Branch 0 taken 477 times.
✓ Branch 1 taken 1429 times.
1906 if (*defval == '"') {
1033 477 defval++; // skip opening quote
1034
3/4
✓ Branch 0 taken 6193 times.
✓ Branch 1 taken 477 times.
✓ Branch 2 taken 6193 times.
✗ Branch 3 not taken.
6670 while (*defval != '"' && *defval != 0)
1035 6193 cleaned_defval += *defval++;
1036
1037
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 477 times.
477 if (*defval == 0) {
1038 asar_throw_error(pass, error_type_null, error_id_mismatched_quotes);
1039 }
1040 477 defval++; // skip closing quote
1041
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 477 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 477 times.
477 while (*defval == ' ' || *defval == '\t') defval++; // skip whitespace
1042
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 477 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
477 if (*defval != 0 && *defval != '\n')
1043 asar_throw_error(pass, error_type_null, error_id_stddefine_after_closing_quote);
1044
1045
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 477 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
477 if (clidefines.exists(define_name)) asar_throw_error(pass, error_type_null, error_id_cmdl_define_override, "Std define", define_name.data());
1046
1/2
✓ Branch 0 taken 477 times.
✗ Branch 1 not taken.
477 clidefines.create(define_name) = cleaned_defval;
1047 477 continue;
1048 }
1049 else
1050 {
1051 // slightly hacky way to remove trailing whitespace
1052 714 const char* defval_end = strchr(defval, '\n'); // slightly hacky way to get end of string or newline
1053
1/2
✓ Branch 0 taken 1429 times.
✗ Branch 1 not taken.
1429 if (!defval_end) defval_end = strchr(defval, 0);
1054 1429 defval_end--;
1055
3/4
✓ Branch 0 taken 479 times.
✓ Branch 1 taken 1429 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1429 times.
1908 while (*defval_end == ' ' || *defval_end == '\t') defval_end--;
1056
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1429 times.
1429 cleaned_defval = string(defval, (int)(defval_end - defval + 1));
1057
1058
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1429 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1429 if (clidefines.exists(define_name)) asar_throw_error(pass, error_type_null, error_id_cmdl_define_override, "Std define", define_name.data());
1059
1/2
✓ Branch 0 taken 1429 times.
✗ Branch 1 not taken.
1429 clidefines.create(define_name) = cleaned_defval;
1060 1429 continue;
1061 1429 }
1062
1063 2861 }
1064 477 free(content);
1065 }
1066 }
1067
1068 bool checksum_fix_enabled = true;
1069 bool force_checksum_fix = false;
1070
1071 #define cfree(x) free((void*)x)
1072 414 static void clearmacro(const string & key, macrodata* & macro)
1073 {
1074 (void)key;
1075 414 freemacro(macro);
1076 414 }
1077
1078 844 static void clearfile(const string & key, sourcefile& filecontent)
1079 {
1080 (void)key;
1081 844 cfree(filecontent.data);
1082 844 cfree(filecontent.contents);
1083 844 }
1084 #undef cfree
1085
1086
1/2
✓ Branch 0 taken 4564 times.
✗ Branch 1 not taken.
4564 static void adddefine(const string & key, string & value)
1087 {
1088
1/2
✓ Branch 0 taken 4564 times.
✗ Branch 1 not taken.
4564 if (!defines.exists(key)) defines.create(key) = value;
1089 4564 }
1090
1091 static string symbolfile;
1092
1093 322 static void printsymbol_wla(const string& key, snes_label& label)
1094 {
1095 644 string line = hex((label.pos & 0xFF0000)>>16, 2)+":"+hex(label.pos & 0xFFFF, 4)+" "+key+"\n";
1096 322 symbolfile += line;
1097 322 }
1098
1099 static void printsymbol_nocash(const string& key, snes_label& label)
1100 {
1101 string line = hex(label.pos & 0xFFFFFF, 8)+" "+key+"\n";
1102 symbolfile += line;
1103 }
1104
1105 168 string create_symbols_file(string format, uint32_t romCrc){
1106 168 format = lower(format);
1107 symbolfile = "";
1108
1/2
✓ Branch 0 taken 168 times.
✗ Branch 1 not taken.
168 if(format == "wla")
1109 {
1110 symbolfile = "; wla symbolic information file\n";
1111 168 symbolfile += "; generated by asar\n";
1112
1113 168 symbolfile += "\n[labels]\n";
1114 168 labels.each(printsymbol_wla);
1115
1116 168 symbolfile += "\n[source files]\n";
1117 const autoarray<AddressToLineMapping::FileInfo>& addrToLineFileList = addressToLineMapping.getFileList();
1118
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 168 times.
346 for (int i = 0; i < addrToLineFileList.count; ++i)
1119 {
1120 char addrToFileListStr[256];
1121 178 snprintf(addrToFileListStr, 256, "%.4x %.8x %s\n",
1122 i,
1123 178 addrToLineFileList[i].fileCrc,
1124 addrToLineFileList[i].filename.data()
1125 );
1126 178 symbolfile += addrToFileListStr;
1127 }
1128
1129 168 symbolfile += "\n[rom checksum]\n";
1130 {
1131 char romCrcStr[32];
1132 168 snprintf(romCrcStr, 32, "%.8x\n",
1133 romCrc
1134 );
1135 168 symbolfile += romCrcStr;
1136 }
1137
1138 168 symbolfile += "\n[addr-to-line mapping]\n";
1139 const autoarray<AddressToLineMapping::AddrToLineInfo>& addrToLineInfo = addressToLineMapping.getAddrToLineInfo();
1140
2/2
✓ Branch 0 taken 5244 times.
✓ Branch 1 taken 168 times.
5412 for (int i = 0; i < addrToLineInfo.count; ++i)
1141 {
1142 char addrToLineStr[32];
1143 5244 snprintf(addrToLineStr, 32, "%.2x:%.4x %.4x:%.8x\n",
1144 5244 (addrToLineInfo[i].addr & 0xFF0000) >> 16,
1145 5244 addrToLineInfo[i].addr & 0xFFFF,
1146 5244 addrToLineInfo[i].fileIdx & 0xFFFF,
1147 5244 addrToLineInfo[i].line & 0xFFFFFFFF
1148 );
1149 5244 symbolfile += addrToLineStr;
1150 }
1151
1152 }
1153 else if (format == "nocash")
1154 {
1155 symbolfile = ";no$sns symbolic information file\n";
1156 symbolfile += ";generated by asar\n";
1157 symbolfile += "\n";
1158 labels.each(printsymbol_nocash);
1159 }
1160 168 return symbolfile;
1161 }
1162
1163
1164 6 bool in_top_level_file()
1165 {
1166 int num_files = 0;
1167
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 6 times.
30 for (int i = callstack.count-1; i >= 0; --i)
1168 {
1169
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 18 times.
24 if (callstack[i].type == callstack_entry_type::FILE)
1170 {
1171 6 num_files++;
1172
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (num_files > 1) break;
1173 }
1174 }
1175 6 return (num_files <= 1);
1176 }
1177
1178 36229 const char* get_current_file_name()
1179 {
1180
2/2
✓ Branch 0 taken 135272 times.
✓ Branch 1 taken 2125 times.
137397 for (int i = callstack.count-1; i >= 0; --i)
1181 {
1182
2/2
✓ Branch 0 taken 34104 times.
✓ Branch 1 taken 101168 times.
135272 if (callstack[i].type == callstack_entry_type::FILE)
1183 34104 return callstack[i].content.raw();
1184 }
1185 return nullptr;
1186 }
1187
1188 26239 int get_current_line()
1189 {
1190
2/2
✓ Branch 0 taken 77606 times.
✓ Branch 1 taken 21 times.
77627 for (int i = callstack.count-1; i >= 0; --i)
1191 {
1192
2/2
✓ Branch 0 taken 26218 times.
✓ Branch 1 taken 51388 times.
103824 if (callstack[i].type == callstack_entry_type::LINE) return callstack[i].lineno;
1193 }
1194 return -1;
1195 }
1196
1197 700 const char* get_current_block()
1198 {
1199
2/2
✓ Branch 0 taken 705 times.
✓ Branch 1 taken 31 times.
736 for (int i = callstack.count-1; i >= 0; --i)
1200 {
1201
4/4
✓ Branch 0 taken 537 times.
✓ Branch 1 taken 168 times.
✓ Branch 2 taken 501 times.
✓ Branch 3 taken 36 times.
1911 if (callstack[i].type == callstack_entry_type::LINE || callstack[i].type == callstack_entry_type::BLOCK) return callstack[i].content.raw();
1202 }
1203 return nullptr;
1204 }
1205
1206
1207 733 void reseteverything()
1208 {
1209 377 string str;
1210 733 labels.reset();
1211 733 defines.reset();
1212
1/2
✓ Branch 0 taken 733 times.
✗ Branch 1 not taken.
733 builtindefines.each(adddefine);
1213
1/2
✓ Branch 0 taken 733 times.
✗ Branch 1 not taken.
733 clidefines.each(adddefine);
1214 733 structs.reset();
1215
1216
1/2
✓ Branch 0 taken 733 times.
✗ Branch 1 not taken.
733 macros.each(clearmacro);
1217 733 macros.reset();
1218
1219
1/2
✓ Branch 0 taken 733 times.
✗ Branch 1 not taken.
733 filecontents.each(clearfile);
1220 733 filecontents.reset();
1221
1222 733 writtenblocks.reset();
1223
1224 733 optimizeforbank=-1;
1225 733 optimize_dp = optimize_dp_flag::NONE;
1226 733 dp_base = 0;
1227 733 optimize_address = optimize_address_flag::DEFAULT;
1228
1229
1/2
✓ Branch 0 taken 733 times.
✗ Branch 1 not taken.
733 closecachedfiles();
1230
1231 733 incsrcdepth=0;
1232 733 label_counter = 0;
1233 733 errored = false;
1234 733 checksum_fix_enabled = true;
1235 733 force_checksum_fix = false;
1236
1237 733 in_macro_def = 0;
1238
1239 #ifndef ASAR_SHARED
1240 238 free(const_cast<unsigned char*>(romdata_r));
1241 #endif
1242
1243 733 callstack.reset();
1244 733 simple_callstacks = true;
1245 #undef free
1246 733 }
1247