asar coverage - build #265


src/asar/
File: src/asar/main.cpp
Date: 2025-02-28 06:59:41
Lines:
614/686
89.5%
Functions:
36/37
97.3%
Branches:
699/1105
63.3%

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 <algorithm>
11 #include <ctime>
12 // randomdude999: remember to also update the .rc files (in res/windows/) when changing this.
13 // Couldn't find a way to automate this without shoving the version somewhere in the CMake files
14 const int asarver_maj=2;
15 const int asarver_min=0;
16 const int asarver_bug=0;
17 const bool asarver_beta=true;
18
19 #ifdef _I_RELEASE
20 extern char blockbetareleases[(!asarver_beta)?1:-1];
21 #endif
22 #ifdef _I_DEBUG
23 extern char blockreleasedebug[(asarver_beta)?1:-1];
24 #endif
25
26 unsigned const char * romdata_r;
27 int romlen_r;
28
29 int pass;
30
31 int optimizeforbank=-1;
32 int optimize_dp = optimize_dp_flag::ALWAYS;
33 int dp_base = 0;
34 int optimize_address = optimize_address_flag::MIRRORS;
35
36 autoarray<callstack_entry> callstack;
37
38 bool errored=false;
39 bool ignoretitleerrors=false;
40
41 int recursioncount=0;
42
43 virtual_filesystem* filesystem = nullptr;
44
45 AddressToLineMapping addressToLineMapping;
46
47 282 int get_version_int()
48 {
49 282 return asarver_maj * 10000 + asarver_min * 100 + asarver_bug;
50 }
51
52 bool setmapper()
53 {
54 int maxscore=-99999;
55 mapper_t bestmap=lorom;
56 mapper_t maps[]={lorom, hirom, exlorom, exhirom};
57 for (size_t mapid=0;mapid<sizeof(maps)/sizeof(maps[0]);mapid++)
58 {
59 mapper=maps[mapid];
60 int score=0;
61 int highbits=0;
62 bool foundnull=false;
63 for (int i=0;i<21;i++)
64 {
65 unsigned char c=romdata[snestopc(0x00FFC0+i)];
66 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
67 if (c>=128) highbits++;
68 else if (is_upper(c)) score+=3;
69 else if (c==' ') score+=2;
70 else if (is_digit(c)) score+=1;
71 else if (is_lower(c)) score+=1;
72 else if (c=='-') score+=1;
73 else if (!c) foundnull=true;
74 else score-=3;
75 }
76 if (highbits>0 && highbits<=14) score-=21;//high bits set on some, but not all, bytes = unlikely to be a ROM
77 if ((romdata[snestopc(0x00FFDE)]^romdata[snestopc(0x00FFDC)])!=0xFF ||
78 (romdata[snestopc(0x00FFDF)]^romdata[snestopc(0x00FFDD)])!=0xFF) score=-99999;//checksum doesn't match up to 0xFFFF? Not a ROM.
79 //too lazy to check the real checksum
80 if (score>maxscore)
81 {
82 maxscore=score;
83 bestmap=mapper;
84 }
85 }
86 mapper=bestmap;
87
88 //detect oddball mappers
89 int mapperbyte=romdata[snestopc(0x00FFD5)];
90 int romtypebyte=romdata[snestopc(0x00FFD6)];
91 if (mapper==lorom)
92 {
93 if (mapperbyte==0x23 && (romtypebyte==0x32 || romtypebyte==0x34 || romtypebyte==0x35)) mapper=sa1rom;
94 }
95 return (maxscore>=0);
96 }
97
98
99 bool simple_callstacks = true;
100
101 // Shortens target_path to a relative path, but only if it resides
102 // within base_path or a child directory of it.
103 5185 static string shorten_to_relative_path(const char* base_path, const char* target_path)
104 {
105
3/4
✓ Branch 0 taken 3605 times.
✓ Branch 1 taken 1580 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 183 times.
5185 if (stribegin(target_path, base_path)) target_path += strlen(base_path);
106 5185 return target_path;
107 }
108
109 5185 static string get_top_level_directory()
110 {
111 5185 string top_level_file_dir;
112
1/2
✓ Branch 0 taken 5185 times.
✗ Branch 1 not taken.
5185 for (int i = 0; i < callstack.count; ++i)
113 {
114
2/4
✓ Branch 0 taken 5185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5185 times.
✗ Branch 3 not taken.
5185 if (callstack[i].type == callstack_entry_type::FILE)
115 {
116
2/4
✓ Branch 0 taken 5185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5185 times.
✗ Branch 3 not taken.
5185 top_level_file_dir = dir(callstack[i].content);
117 5185 break;
118 }
119 }
120 5185 return top_level_file_dir;
121 }
122
123 5143 static string generate_call_details_string(const char* current_block, const char* current_call, int indentation, bool add_lines)
124 {
125 5143 string e;
126
4/4
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 5081 times.
✓ Branch 2 taken 50 times.
✓ Branch 3 taken 12 times.
5143 if (current_block != nullptr || current_call != nullptr)
127 {
128 5131 string indent;
129
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5127 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
5131 if (add_lines) indent += "|";
130
3/4
✓ Branch 0 taken 1044 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1044 times.
✓ Branch 3 taken 5131 times.
6175 for (; indentation > 0; --indentation) indent += " ";
131
132
8/14
✓ Branch 0 taken 5081 times.
✓ Branch 1 taken 50 times.
✓ Branch 2 taken 5081 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5081 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5081 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5081 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 5081 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 5081 times.
✗ Branch 13 not taken.
5131 if (current_block != nullptr) e += STR "\n"+indent+"in block: ["+current_block+"]";
133
8/14
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 5081 times.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 50 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 50 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 50 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 50 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 50 times.
✗ Branch 13 not taken.
5131 if (current_call != nullptr) e += STR "\n"+indent+"in macro call: [%"+current_call+"]";
134 5131 }
135 5143 return e;
136 }
137
138 5185 static string get_pretty_filename(const char* current_file)
139 {
140 // RPG Hacker: One could make an argument that we shouldn't shorten paths
141 // here, since some IDEs support jumping to files by double-clicking their
142 // paths. However, AFAIK, no IDE supports this for Asar yet, and if it's
143 // ever desired, we could just make it a command line option. Until then,
144 // I think it's more important to optimize for pretty command line display.
145
2/4
✓ Branch 0 taken 5185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5185 times.
✗ Branch 3 not taken.
10370 return shorten_to_relative_path(get_top_level_directory(), current_file);
146 }
147
148 307 static string generate_filename_and_line(const char* current_file, int current_line_no)
149 {
150
1/2
✓ Branch 0 taken 307 times.
✗ Branch 1 not taken.
614 return STR current_file
151
15/28
✓ Branch 0 taken 295 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 295 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 295 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 295 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 12 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 307 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 295 times.
✓ Branch 13 taken 12 times.
✓ Branch 14 taken 295 times.
✓ Branch 15 taken 12 times.
✓ Branch 16 taken 152 times.
✓ Branch 17 taken 6 times.
✓ Branch 18 taken 152 times.
✓ Branch 19 taken 6 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
921 + (current_line_no>=0?STR ":"+dec(current_line_no+1):"");
152 }
153
154 4 static string format_stack_line(const printable_callstack_entry& entry, int stack_frame_index)
155 {
156
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 string indent = "\n| ";
157
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 indent += dec(stack_frame_index);
158
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 indent += ": ";
159 // RPG Hacker: We'll probably never have a call stack in the
160 // hundreds even, so this very specific, lazy solution suffices.
161
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 if (stack_frame_index < 100) indent += " ";
162
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 if (stack_frame_index < 10) indent += " ";
163 return indent
164
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 + generate_filename_and_line(entry.prettypath, entry.lineno)
165
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
12 + entry.details;
166 4 }
167
168 4882 static 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)
169 {
170 4882 printable_callstack_entry new_entry;
171
1/2
✓ Branch 0 taken 4882 times.
✗ Branch 1 not taken.
4882 new_entry.fullpath = current_file;
172
1/2
✓ Branch 0 taken 4882 times.
✗ Branch 1 not taken.
4882 new_entry.prettypath = get_pretty_filename(current_file);
173 4882 new_entry.lineno = current_line_no;
174
2/4
✓ Branch 0 taken 4882 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4882 times.
✗ Branch 3 not taken.
4882 new_entry.details = generate_call_details_string(current_block, current_call, indentation, add_lines).raw();
175
1/2
✓ Branch 0 taken 4882 times.
✗ Branch 1 not taken.
4882 out->append(new_entry);
176 6490 }
177
178 262 void get_current_line_details(string* location, string* details, bool exclude_block)
179 {
180 262 const char* current_file = nullptr;
181 262 const char* current_block = nullptr;
182 262 const char* current_call = nullptr;
183 262 int current_line_no = -1;
184
2/2
✓ Branch 0 taken 892 times.
✓ Branch 1 taken 1 times.
893 for (int i = callstack.count-1; i >= 0 ; --i)
185 {
186
3/5
✓ Branch 0 taken 261 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 249 times.
✓ Branch 3 taken 382 times.
✗ Branch 4 not taken.
892 switch (callstack[i].type)
187 {
188 261 case callstack_entry_type::FILE:
189 261 current_file = callstack[i].content;
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 261 times.
261 if (exclude_block) current_block = nullptr;
191
2/4
✓ Branch 0 taken 261 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 261 times.
✗ Branch 3 not taken.
261 *location = generate_filename_and_line(get_pretty_filename(current_file), current_line_no);
192
1/2
✓ Branch 0 taken 261 times.
✗ Branch 1 not taken.
261 *details = generate_call_details_string(current_block, current_call, 4, false);
193 261 return;
194 case callstack_entry_type::MACRO_CALL:
195 if (current_call == nullptr) current_call = callstack[i].content;
196 break;
197 249 case callstack_entry_type::LINE:
198
3/4
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 193 times.
✓ Branch 2 taken 56 times.
✗ Branch 3 not taken.
249 if (current_block == nullptr && current_call == nullptr) current_block = callstack[i].content;
199
1/2
✓ Branch 0 taken 249 times.
✗ Branch 1 not taken.
249 if (current_line_no == -1) current_line_no = callstack[i].lineno;
200 249 break;
201 382 case callstack_entry_type::BLOCK:
202
2/2
✓ Branch 0 taken 193 times.
✓ Branch 1 taken 189 times.
382 if (current_block == nullptr) current_block = callstack[i].content;
203 382 break;
204 }
205 }
206 1 *location = "";
207 1 *details = "";
208 }
209
210 264 void get_full_printable_callstack(autoarray<printable_callstack_entry>* out, int indentation, bool add_lines)
211 {
212 264 out->reset();
213 264 const char* current_file = nullptr;
214 264 const char* current_block = nullptr;
215 264 const char* current_call = nullptr;
216 264 int current_line_no = -1;
217
2/2
✓ Branch 0 taken 20478 times.
✓ Branch 1 taken 264 times.
20742 for (int i = 0; i < callstack.count; ++i)
218 {
219
4/5
✓ Branch 0 taken 5145 times.
✓ Branch 1 taken 50 times.
✓ Branch 2 taken 5133 times.
✓ Branch 3 taken 10150 times.
✗ Branch 4 not taken.
20478 switch (callstack[i].type)
220 {
221 5145 case callstack_entry_type::FILE:
222
2/2
✓ Branch 0 taken 4882 times.
✓ Branch 1 taken 263 times.
5145 if (current_file != nullptr)
223 {
224 4882 push_stack_line(out, current_file, current_block, current_call, current_line_no, indentation, add_lines);
225 }
226 5145 current_file = callstack[i].content;
227 5145 current_block = nullptr;
228 5145 current_call = nullptr;
229 5145 current_line_no = -1;
230 5145 break;
231 50 case callstack_entry_type::MACRO_CALL:
232 50 current_block = nullptr;
233 50 current_call = callstack[i].content;
234 50 break;
235 5133 case callstack_entry_type::LINE:
236 5133 current_line_no = callstack[i].lineno;
237 5133 current_block = callstack[i].content;
238 5133 break;
239 10150 case callstack_entry_type::BLOCK:
240 10150 current_block = callstack[i].content;
241 10150 break;
242 }
243 }
244 264 }
245
246 2 static string get_full_callstack()
247 {
248 2 autoarray<printable_callstack_entry> printable_stack;
249
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 get_full_printable_callstack(&printable_stack, 12, true);
250
251 2 string e;
252
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (printable_stack.count > 0)
253 {
254
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 e += "\nFull call stack:";
255
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (int i = printable_stack.count-1; i >= 0; --i)
256 {
257
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 e += format_stack_line(printable_stack[i], i);
258 }
259 }
260 4 return e;
261 2 }
262
263 // RPG Hacker: This function essetially replicates classic Asar behavior
264 // of only printing a single macro call below the current level.
265 260 static string get_simple_callstack()
266 {
267 int i;
268 260 const char* current_call = nullptr;
269
2/2
✓ Branch 0 taken 20254 times.
✓ Branch 1 taken 218 times.
20472 for (i = callstack.count-1; i >= 0 ; --i)
270 {
271
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 20212 times.
20254 if (callstack[i].type == callstack_entry_type::MACRO_CALL)
272 {
273 42 current_call = callstack[i].content;
274 42 break;
275 }
276 }
277
278 260 const char* current_file = nullptr;
279 260 int current_line_no = -1;
280
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 218 times.
260 if (current_call != nullptr)
281 {
282 42 bool stop = false;
283
1/2
✓ Branch 0 taken 168 times.
✗ Branch 1 not taken.
168 for (int j = i-1; j >= 0 ; --j)
284 {
285
3/5
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 84 times.
✗ Branch 4 not taken.
168 switch (callstack[j].type)
286 {
287 42 case callstack_entry_type::FILE:
288
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (current_file != nullptr)
289 {
290 stop = true;
291 break;
292 }
293 42 current_file = callstack[j].content;
294 42 break;
295 case callstack_entry_type::MACRO_CALL:
296 stop = true;
297 break;
298 42 case callstack_entry_type::LINE:
299
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (current_line_no == -1) current_line_no = callstack[j].lineno;
300 42 break;
301 84 case callstack_entry_type::BLOCK:
302 84 break;
303 }
304
305
3/4
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 126 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
168 if (current_file != nullptr && current_line_no != -1) stop = true;
306
307
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 126 times.
168 if (stop) break;
308 }
309 }
310
311 260 string e;
312
3/4
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 218 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
260 if (current_call != nullptr && current_file != nullptr)
313 {
314
4/8
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 42 times.
✗ Branch 7 not taken.
84 e += STR "\n called from: " + generate_filename_and_line(get_pretty_filename(current_file), current_line_no)
315
4/8
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 42 times.
✗ Branch 7 not taken.
105 + ": [%" + current_call + "]";
316 }
317 260 return e;
318 }
319
320 262 string get_callstack()
321 {
322
4/4
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 134 times.
✓ Branch 2 taken 132 times.
✓ Branch 3 taken 2 times.
262 if (simple_callstacks)
323 260 return get_simple_callstack();
324 else
325 2 return get_full_callstack();
326 }
327
328 36 asar_error_id vfile_error_to_error_id(virtual_file_error vfile_error)
329 {
330
2/5
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
36 switch (vfile_error)
331 {
332 12 case vfe_doesnt_exist:
333 12 return error_id_file_not_found;
334 case vfe_access_denied:
335 return error_id_failed_to_open_file_access_denied;
336 24 case vfe_not_regular_file:
337 24 return error_id_failed_to_open_not_regular_file;
338 case vfe_unknown:
339 case vfe_none:
340 case vfe_num_errors:
341 return error_id_failed_to_open_file;
342 }
343
344 return error_id_failed_to_open_file;
345 }
346
347 36 virtual_file_error asar_get_last_io_error()
348 {
349
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 if (filesystem != nullptr)
350 {
351 36 return filesystem->get_last_error();
352 }
353
354 return vfe_unknown;
355 }
356
357 639 int getlenforlabel(snes_label thislabel, bool exists)
358 {
359 639 unsigned int bank = thislabel.pos>>16;
360 639 unsigned int word = thislabel.pos&0xFFFF;
361 639 bool lblfreespace = thislabel.freespace_id > 0;
362 unsigned int relaxed_bank;
363
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 615 times.
639 if(optimizeforbank >= 0) {
364 24 relaxed_bank = optimizeforbank;
365 } else {
366
2/2
✓ Branch 0 taken 487 times.
✓ Branch 1 taken 128 times.
615 if(freespaceid == 0) {
367 487 relaxed_bank = snespos >> 16;
368 } else {
369 128 int target_bank = freespaces[freespaceid].bank;
370
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 44 times.
128 if(target_bank == -2) relaxed_bank = 0;
371
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 else if(target_bank == -1) relaxed_bank = 0x40;
372 else relaxed_bank = target_bank;
373 }
374 }
375
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 639 times.
639 if (!exists)
376 {
377 return 2;
378 }
379
6/8
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 627 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
639 else if((optimize_dp == optimize_dp_flag::RAM) && bank == 0x7E && (word-dp_base < 0x100) && !lblfreespace)
380 {
381 6 return 1;
382 }
383
8/10
✓ Branch 0 taken 419 times.
✓ Branch 1 taken 214 times.
✓ Branch 2 taken 419 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 419 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 91 times.
✓ Branch 7 taken 328 times.
✓ Branch 8 taken 23 times.
✓ Branch 9 taken 68 times.
633 else if(optimize_dp == optimize_dp_flag::ALWAYS && (bank == 0x7E || !(bank & 0x40)) && (word-dp_base < 0x100) && !lblfreespace)
384 {
385 23 return 1;
386 }
387 610 else if (
388 // if we should optimize ram accesses...
389
4/4
✓ Branch 0 taken 574 times.
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 466 times.
✓ Branch 3 taken 108 times.
610 (optimize_address == optimize_address_flag::RAM || optimize_address == optimize_address_flag::MIRRORS)
390 // and we're in a bank with ram mirrors... (optimizeforbank=0x7E is checked later)
391
2/2
✓ Branch 0 taken 470 times.
✓ Branch 1 taken 32 times.
502 && !(relaxed_bank & 0x40)
392 // and the label is in low RAM
393
4/6
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 452 times.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
470 && bank == 0x7E && word < 0x2000 && !lblfreespace)
394 {
395 18 return 2;
396 }
397 592 else if (
398 // if we should optimize mirrors...
399
2/2
✓ Branch 0 taken 460 times.
✓ Branch 1 taken 132 times.
592 optimize_address == optimize_address_flag::MIRRORS
400 // we're in a bank with ram mirrors...
401
2/2
✓ Branch 0 taken 434 times.
✓ Branch 1 taken 26 times.
460 && !(relaxed_bank & 0x40)
402 // and the label is in a mirrored section
403
5/6
✓ Branch 0 taken 434 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
✓ Branch 3 taken 344 times.
✓ Branch 4 taken 24 times.
✓ Branch 5 taken 66 times.
434 && !(bank & 0x40) && word < 0x8000 && !lblfreespace)
404 {
405 24 return 2;
406 }
407
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 544 times.
568 else if (optimizeforbank>=0)
408 {
409 // if optimizing for a specific bank:
410 // if the label is in freespace, never optimize
411
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (thislabel.freespace_id > 0) return 3;
412
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
24 else if (bank==(unsigned int)optimizeforbank) return 2;
413 12 else return 3;
414 }
415
4/4
✓ Branch 0 taken 390 times.
✓ Branch 1 taken 154 times.
✓ Branch 2 taken 54 times.
✓ Branch 3 taken 336 times.
544 else if (thislabel.freespace_id > 0 || freespaceid > 0)
416 {
417 // optimize only if the label is in the same freespace
418 // TODO: check whether they're pinned to the same bank
419
2/2
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 32 times.
208 if (thislabel.freespace_id != freespaceid) return 3;
420 32 else return 2;
421 }
422
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 318 times.
336 else if ((int)bank != snespos >> 16){ return 3; }
423 318 else { return 2;}
424 }
425
426
427 315 bool is_hex_constant(const char* str){
428
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 99 times.
315 if (*str=='$')
429 {
430 216 str++;
431
2/2
✓ Branch 0 taken 600 times.
✓ Branch 1 taken 216 times.
816 while(is_xdigit(*str)) {
432 600 str++;
433 }
434
1/2
✓ Branch 0 taken 216 times.
✗ Branch 1 not taken.
216 if(*str=='\0'){
435 216 return true;
436 }
437 }
438 99 return false;
439 }
440
441 struct strcompare {
442 bool operator() (const char * lhs, const char * rhs) const
443 {
444 return strcmp(lhs, rhs)<0;
445 }
446 };
447
448 struct stricompare {
449 bool operator() (const char * lhs, const char * rhs) const
450 {
451 return stricmp(lhs, rhs)<0;
452 }
453 };
454
455 struct sourcefile {
456 char *data;
457 char** contents;
458 int numlines;
459 };
460
461 static assocarr<sourcefile> filecontents;
462 assocarr<string> defines;
463 // needs to be separate because defines is reset between parsing arguments and patching
464 assocarr<string> clidefines;
465 assocarr<string> builtindefines;
466
467 202 bool validatedefinename(const char * name)
468 {
469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 202 times.
202 if (!name[0]) return false;
470
2/2
✓ Branch 0 taken 918 times.
✓ Branch 1 taken 202 times.
1120 for (int i = 0;name[i];i++)
471 {
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 918 times.
918 if (!is_ualnum(name[i])) return false;
473 }
474
475 202 return true;
476 }
477
478 60925 void resolvedefines(string& out, const char * start)
479 {
480
2/2
✓ Branch 0 taken 60923 times.
✓ Branch 1 taken 2 times.
60925 recurseblock rec;
481 60923 const char * here=start;
482
4/4
✓ Branch 0 taken 25906 times.
✓ Branch 1 taken 35017 times.
✓ Branch 2 taken 24550 times.
✓ Branch 3 taken 5259 times.
60923 if (!strchr(here, '!'))
483 {
484
1/2
✓ Branch 0 taken 50456 times.
✗ Branch 1 not taken.
50456 out += here;
485 50456 return;
486 }
487
2/2
✓ Branch 0 taken 71940 times.
✓ Branch 1 taken 10436 times.
82376 while (*here)
488 {
489
4/4
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 71892 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 42 times.
71940 if (here[0] == '\\' && here[1] == '\\')
490 {
491 // allow using \\ as escape sequence
492
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6 if (in_macro_def > 0) out += "\\";
493
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 out += "\\";
494 6 here += 2;
495 }
496
3/4
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 71892 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
71934 else if (here[0] == '\\' && here[1] == '!')
497 {
498 // allow using \! to escape !
499
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
42 if (in_macro_def > 0) out += "\\";
500
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 out+="!";
501 42 here += 2;
502 }
503
2/2
✓ Branch 0 taken 11241 times.
✓ Branch 1 taken 60651 times.
71892 else if (*here=='!')
504 {
505
10/10
✓ Branch 0 taken 5472 times.
✓ Branch 1 taken 5769 times.
✓ Branch 2 taken 3399 times.
✓ Branch 3 taken 2073 times.
✓ Branch 4 taken 2586 times.
✓ Branch 5 taken 813 times.
✓ Branch 6 taken 120 times.
✓ Branch 7 taken 2466 times.
✓ Branch 8 taken 12 times.
✓ Branch 9 taken 108 times.
11241 bool first=(here==start || (here>=start+4 && here[-1]==' ' && here[-2]==':' && here[-3]==' '));//check if it's the start of a block
506 11241 string defname;
507 11241 here++;
508
509 11241 int depth = 0;
510
2/2
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 11241 times.
11355 for (const char* depth_str = here; *depth_str=='^'; depth_str++)
511 {
512 114 depth++;
513 }
514 11241 here += depth;
515
516
2/2
✓ Branch 0 taken 1323 times.
✓ Branch 1 taken 9918 times.
11241 if (depth != in_macro_def)
517 {
518
1/2
✓ Branch 0 taken 1323 times.
✗ Branch 1 not taken.
1323 out += '!';
519
3/4
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 66 times.
✓ Branch 3 taken 1323 times.
1389 for (int i=0; i < depth; ++i) out += '^';
520
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1305 times.
1323 if (depth > in_macro_def) asar_throw_error(0, error_type_line, error_id_invalid_depth_resolve, "define", "define", depth, in_macro_def);
521 1305 continue;
522 1305 }
523
524
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 9810 times.
9918 if (*here=='{')
525 {
526 108 here++;
527 108 string unprocessedname;
528 108 int braces=1;
529 while (true)
530 {
531
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 1080 times.
1152 if (*here=='{') braces++;
532
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 972 times.
1152 if (*here=='}') braces--;
533
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1152 times.
1152 if (!*here) asar_throw_error(0, error_type_line, error_id_mismatched_braces);
534
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 1044 times.
1152 if (!braces) break;
535
1/2
✓ Branch 0 taken 1044 times.
✗ Branch 1 not taken.
1044 unprocessedname+=*here++;
536 }
537 108 here++;
538
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 resolvedefines(defname, unprocessedname);
539
3/4
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 54 times.
108 if (!validatedefinename(defname)) asar_throw_error(0, error_type_line, error_id_invalid_define_name);
540 108 }
541 else
542 {
543
3/4
✓ Branch 0 taken 49786 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 49786 times.
✓ Branch 3 taken 9810 times.
59596 while (is_ualnum(*here)) defname+=*here++;
544 }
545
546
2/2
✓ Branch 0 taken 4968 times.
✓ Branch 1 taken 4950 times.
9918 if (first)
547 {
548 enum {
549 null,
550 append,
551 expand,
552 domath,
553 setifnotset,
554 } mode;
555 if(0);
556
2/2
✓ Branch 0 taken 522 times.
✓ Branch 1 taken 4749 times.
5271 else if (stribegin(here, " = ")) { here+=3; mode=null; }
557
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 4647 times.
4749 else if (stribegin(here, " += ")) { here+=4; mode=append; }
558
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 4590 times.
4647 else if (stribegin(here, " := ")) { here+=4; mode=expand; }
559
2/2
✓ Branch 0 taken 1926 times.
✓ Branch 1 taken 2664 times.
4590 else if (stribegin(here, " #= ")) { here+=4; mode=domath; }
560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2664 times.
2664 else if (stribegin(here, " ?= ")) { here+=4; mode=setifnotset; }
561 2664 else goto notdefineset;
562 2607 string val;
563
2/2
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 2418 times.
2607 if (*here=='"')
564 {
565 189 here++;
566 while (true)
567 {
568
2/2
✓ Branch 0 taken 195 times.
✓ Branch 1 taken 828 times.
1023 if (*here=='"')
569 {
570
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 189 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
195 if (!here[1] || here[1]==' ') break;
571
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 else if (here[1]=='"') here++;
572 else asar_throw_error(0, error_type_line, error_id_broken_define_declaration);
573 }
574
1/2
✓ Branch 0 taken 834 times.
✗ Branch 1 not taken.
834 val+=*here++;
575 }
576 189 here++;
577 }
578 else
579 {
580
4/6
✓ Branch 0 taken 16635 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16635 times.
✓ Branch 3 taken 2418 times.
✓ Branch 4 taken 16635 times.
✗ Branch 5 not taken.
19053 while (*here && *here!=' ') val+=*here++;
581 }
582 //if (strqchr(val.data(), ';')) *strqchr(val.data(), ';')=0;
583
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 2607 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2607 times.
2607 if (*here && !stribegin(here, " : ")) asar_throw_error(0, error_type_line, error_id_broken_define_declaration);
584 // RPG Hacker: Is it really a good idea to normalize
585 // the content of defines? That kinda violates their
586 // functionality as a string replacement mechanism.
587 //val.qnormalize();
588
589 // RPG Hacker: throw an error if we're trying to overwrite built-in defines.
590
3/4
✓ Branch 0 taken 2607 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 2601 times.
2607 if (builtindefines.exists(defname))
591 {
592 6 asar_throw_error(0, error_type_line, error_id_overriding_builtin_define, defname.data());
593 }
594
595
4/6
✓ Branch 0 taken 516 times.
✓ Branch 1 taken 102 times.
✓ Branch 2 taken 57 times.
✓ Branch 3 taken 1926 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
2601 switch (mode)
596 {
597 516 case null:
598 {
599
2/4
✓ Branch 0 taken 516 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 516 times.
✗ Branch 3 not taken.
516 defines.create(defname) = val;
600 516 break;
601 }
602 102 case append:
603 {
604
2/4
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 102 times.
102 if (!defines.exists(defname)) asar_throw_error(0, error_type_line, error_id_define_not_found, defname.data());
605
2/4
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 102 times.
✗ Branch 3 not taken.
102 string oldval = defines.find(defname);
606
1/2
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
102 val=oldval+val;
607
2/4
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 102 times.
✗ Branch 3 not taken.
102 defines.create(defname) = val;
608 102 break;
609 102 }
610 57 case expand:
611 {
612 57 string newval;
613
1/2
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
57 resolvedefines(newval, val);
614
2/4
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 57 times.
✗ Branch 3 not taken.
57 defines.create(defname) = newval;
615 57 break;
616 57 }
617 1926 case domath:
618 {
619 1926 string newval;
620
1/2
✓ Branch 0 taken 1926 times.
✗ Branch 1 not taken.
1926 resolvedefines(newval, val);
621
1/2
✓ Branch 0 taken 1926 times.
✗ Branch 1 not taken.
1926 double num= getnumdouble(newval);
622
7/8
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1920 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 960 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 3 times.
1926 if (foundlabel && !foundlabel_static) asar_throw_error(0, error_type_line, error_id_define_label_math);
623
2/4
✓ Branch 0 taken 1920 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1920 times.
✗ Branch 3 not taken.
1920 defines.create(defname) = ftostr(num);
624 1920 break;
625 1926 }
626 case setifnotset:
627 {
628 if (!defines.exists(defname)) defines.create(defname) = val;
629 break;
630 }
631 }
632 2607 }
633 else
634 {
635 5982 notdefineset:
636
3/4
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 7242 times.
✓ Branch 2 taken 69 times.
✗ Branch 3 not taken.
7311 if (!defname) out+="!";
637 else
638 {
639
3/4
✓ Branch 0 taken 7242 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 7241 times.
7242 if (!defines.exists(defname)) asar_throw_error(0, error_type_line, error_id_define_not_found, defname.data());
640 else {
641
2/4
✓ Branch 0 taken 7241 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7241 times.
✗ Branch 3 not taken.
7241 string thisone = defines.find(defname);
642
1/2
✓ Branch 0 taken 7241 times.
✗ Branch 1 not taken.
7241 resolvedefines(out, thisone);
643 7241 }
644 }
645 }
646
2/2
✓ Branch 0 taken 9905 times.
✓ Branch 1 taken 1305 times.
11241 }
647
1/2
✓ Branch 0 taken 60651 times.
✗ Branch 1 not taken.
60651 else out+=*here++;
648 }
649
5/8
✓ Branch 0 taken 10436 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 10430 times.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
10436 if (!confirmquotes(out)) { asar_throw_error(0, error_type_null, error_id_mismatched_quotes); out = ""; }
650
2/2
✓ Branch 0 taken 10436 times.
✓ Branch 1 taken 50456 times.
60923 }
651
652 bool moreonline;
653 bool asarverallowed = false;
654
655 49991 void assembleline(const char * fname, int linenum, const char * line, int& single_line_for_tracker)
656 {
657
1/2
✓ Branch 0 taken 49991 times.
✗ Branch 1 not taken.
49991 recurseblock rec;
658
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24307 times.
49991 bool moreonlinetmp=moreonline;
659 // randomdude999: redundant, assemblefile already converted the path to absolute
660 //string absolutepath = filesystem->create_absolute_path("", fname);
661
1/2
✓ Branch 0 taken 49991 times.
✗ Branch 1 not taken.
49991 string absolutepath = fname;
662 49991 single_line_for_tracker = 1;
663 try
664 {
665
1/2
✓ Branch 0 taken 49991 times.
✗ Branch 1 not taken.
49991 string out=line;
666
1/2
✓ Branch 0 taken 49991 times.
✗ Branch 1 not taken.
49991 autoptr<char**> blocks=qsplitstr(out.temp_raw(), " : ");
667 49991 moreonline=true;
668
4/4
✓ Branch 0 taken 26584 times.
✓ Branch 1 taken 70435 times.
✓ Branch 2 taken 25276 times.
✓ Branch 3 taken 22728 times.
97019 for (int block=0;moreonline;block++)
669 {
670 51860 moreonline=(blocks[block+1] != nullptr);
671 try
672 {
673 // it's possible that our input looks something like:
674 // nop : : nop
675 // nop : : : : : nop
676 // also, it's possible that there were empty blocks at the start or end of the line:
677 // : nop :
678 // after qsplit, we still need to deal with possibly a single ": " from a preceding empty block,
679 // and if it's the last block, possibly a following " :".
680
1/2
✓ Branch 0 taken 51860 times.
✗ Branch 1 not taken.
51860 string stripped_block = strip_whitespace(blocks[block]);
681 51860 int i = 0;
682 // if the block starts with ": "
683
6/6
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 51806 times.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 30 times.
✓ Branch 4 taken 24 times.
✓ Branch 5 taken 51836 times.
51860 if(stripped_block[i] == ':' && stripped_block[i+1] == ' ') {
684 24 i++;
685
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 24 times.
48 while(stripped_block[i] == ' ') i++;
686 }
687 // if the block is a single :, skip that too.
688
5/6
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 51830 times.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 30 times.
✓ Branch 5 taken 51830 times.
51860 if(stripped_block[i] == ':' && stripped_block[i+1] == 0) i++;
689
690 // last block - strip trailing " :" if present.
691
12/12
✓ Branch 0 taken 25684 times.
✓ Branch 1 taken 26176 times.
✓ Branch 2 taken 41668 times.
✓ Branch 3 taken 9292 times.
✓ Branch 4 taken 17463 times.
✓ Branch 5 taken 24205 times.
✓ Branch 6 taken 1593 times.
✓ Branch 7 taken 15870 times.
✓ Branch 8 taken 12 times.
✓ Branch 9 taken 28159 times.
✓ Branch 10 taken 6 times.
✓ Branch 11 taken 25270 times.
51860 if(!moreonline && stripped_block.length() >= 2 && stripped_block[stripped_block.length()-2] == ' ' && stripped_block[stripped_block.length()-1] == ':') {
692
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 stripped_block.truncate(stripped_block.length()-2);
693 }
694
695
1/2
✓ Branch 0 taken 51860 times.
✗ Branch 1 not taken.
51860 callstack_push cs_push(callstack_entry_type::BLOCK, stripped_block.data() + i);
696
697
2/2
✓ Branch 0 taken 46694 times.
✓ Branch 1 taken 5166 times.
51860 assembleblock(stripped_block.data() + i, single_line_for_tracker);
698
2/2
✓ Branch 0 taken 46690 times.
✓ Branch 1 taken 4 times.
46694 checkbankcross();
699 57030 }
700
2/2
✓ Branch 0 taken 4832 times.
✓ Branch 1 taken 338 times.
5170 catch (errblock&) {}
701
2/2
✓ Branch 0 taken 30496 times.
✓ Branch 1 taken 16532 times.
47028 if (blocks[block][0]!='\0') asarverallowed=false;
702
2/2
✓ Branch 0 taken 44829 times.
✓ Branch 1 taken 2199 times.
47028 if(single_line_for_tracker == 1) single_line_for_tracker = 0;
703 }
704 54823 }
705
1/2
✓ Branch 0 taken 4832 times.
✗ Branch 1 not taken.
4832 catch (errline&) {}
706 45159 moreonline=moreonlinetmp;
707 77551 }
708
709 int incsrcdepth=0;
710
711 // Returns true if a file is protected via
712 // an "includeonce".
713 5841 bool file_included_once(const char* file)
714 {
715
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 5799 times.
6039 for (int i = 0; i < includeonce.count; ++i)
716 {
717
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 198 times.
240 if (includeonce[i] == file)
718 {
719 42 return true;
720 }
721 }
722
723 5799 return false;
724 }
725
726 autoarray<string> macro_defs;
727 int in_macro_def=0;
728
729 5811 void assemblefile(const char * filename)
730 {
731 5811 incsrcdepth++;
732
2/4
✓ Branch 0 taken 5811 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5811 times.
✗ Branch 3 not taken.
5811 string absolutepath = filesystem->create_absolute_path(get_current_file_name(), filename);
733
734
3/4
✓ Branch 0 taken 5811 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 5769 times.
5811 if (file_included_once(absolutepath))
735 {
736 42 return;
737 }
738
739
1/2
✓ Branch 0 taken 5769 times.
✗ Branch 1 not taken.
5769 callstack_push cs_push(callstack_entry_type::FILE, absolutepath);
740
741 2106 sourcefile file;
742 5769 file.contents = nullptr;
743 5769 file.numlines = 0;
744 5769 int startif=numif;
745
3/4
✓ Branch 0 taken 5769 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 329 times.
✓ Branch 3 taken 5440 times.
5769 if (!filecontents.exists(absolutepath))
746 {
747
2/2
✓ Branch 0 taken 327 times.
✓ Branch 1 taken 2 times.
329 char * temp = readfile(absolutepath, "");
748
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 321 times.
327 if (!temp)
749 {
750
5/12
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 3 times.
✗ Branch 11 not taken.
6 asar_throw_error(0, error_type_null, vfile_error_to_error_id(asar_get_last_io_error()), filename);
751
752 6 return;
753 }
754
1/2
✓ Branch 0 taken 321 times.
✗ Branch 1 not taken.
321 sourcefile& newfile = filecontents.create(absolutepath);
755
1/2
✓ Branch 0 taken 321 times.
✗ Branch 1 not taken.
321 newfile.contents =split(temp, '\n');
756 321 newfile.data = temp;
757
2/2
✓ Branch 0 taken 11739 times.
✓ Branch 1 taken 321 times.
12060 for (int i=0;newfile.contents[i];i++)
758 {
759 11739 newfile.numlines++;
760 11739 char * line = newfile.contents[i];
761 11739 int i_temp = i;
762 char * comment;
763
2/2
✓ Branch 0 taken 3114 times.
✓ Branch 1 taken 11737 times.
14851 while((comment = strqchr(line, ';'))) {
764
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3102 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
3114 if(comment[1] == '[' && comment[2] == '[') {
765 // block comment - find where it ends
766 12 char* theline = comment + 3;
767
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
12 char* comment_end = strstr(theline, "]]");
768
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10 times.
30 while(comment_end == nullptr) {
769 20 i_temp++;
770 20 char* new_line = newfile.contents[i_temp];
771
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 18 times.
20 if(new_line == nullptr) {
772
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 callstack_push cs_push(callstack_entry_type::LINE, line, i);
773
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 asar_throw_error(0, error_type_null, error_id_unclosed_block_comment);
774 // make sure this line is still parsed correctly
775 2 *comment = 0;
776 // but don't go looking at any other lines
777 2 goto break_outer;
778 2 }
779
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
18 comment_end = strstr(new_line, "]]");
780 // this line is itself part of the comment, so ignore it
781 //new_line[0] = 0;
782 // except not like that^, because that will break the
783 // memmove below
784 static char junk[]="";
785 // using a static here should be fine, since if the line
786 // doesn't contain ',' or '\' we won't go mutating it
787 18 newfile.contents[i_temp] = junk;
788 }
789 // comment_end+2 is a valid pointer, since comment_end is
790 // guaranteed to start with ]]
791 10 comment_end += 2;
792 // stitch together the part of the line before the comment,
793 // and the part of the line after it
794
3/6
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5 times.
10 memmove(comment, comment_end, strlen(comment_end) + 1);
795 // and then recheck for ; in the line again...
796 10 } else {
797 3102 *comment = 0;
798 }
799 }
800 11738 break_outer:
801
3/4
✓ Branch 0 taken 11739 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 11735 times.
11739 if (!confirmquotes(line)) {
802
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 callstack_push cs_push(callstack_entry_type::LINE, line, i);
803
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 asar_throw_error(0, error_type_null, error_id_mismatched_quotes);
804 4 line[0] = '\0';
805 4 }
806 11739 newfile.contents[i] = strip_whitespace(line);
807 }
808
2/2
✓ Branch 0 taken 11739 times.
✓ Branch 1 taken 321 times.
12060 for(int i=0;newfile.contents[i];i++)
809 {
810 11739 char* line = newfile.contents[i];
811
2/2
✓ Branch 0 taken 4446 times.
✓ Branch 1 taken 7293 times.
11739 if(!*line) continue;
812
5/6
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 7298 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 3689 times.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
7303 for (int j=1;line[strlen(line) - 1] == ',' && newfile.contents[i+j];j++)
813 {
814 // not using strcat because the source and dest overlap here
815 10 char* otherline = newfile.contents[i+j];
816
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
10 char* line_end = line + strlen(line);
817
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 10 times.
138 while(*otherline) *line_end++ = *otherline++;
818 10 *line_end = '\0';
819 static char nullstr[]="";
820 10 newfile.contents[i+j]=nullstr;
821 }
822 }
823 321 file = newfile;
824 } else { // filecontents.exists(absolutepath)
825
1/2
✓ Branch 0 taken 5440 times.
✗ Branch 1 not taken.
5440 file = filecontents.find(absolutepath);
826 }
827 5761 asarverallowed=true;
828
3/4
✓ Branch 0 taken 46405 times.
✓ Branch 1 taken 927 times.
✓ Branch 2 taken 46405 times.
✗ Branch 3 not taken.
47332 for (int i=0;file.contents[i] && i<file.numlines;i++)
829 {
830 46405 string connectedline;
831
1/2
✓ Branch 0 taken 46405 times.
✗ Branch 1 not taken.
46405 int skiplines = getconnectedlines<char**>(file.contents, i, connectedline);
832
833
2/2
✓ Branch 0 taken 41571 times.
✓ Branch 1 taken 4834 times.
46405 bool was_loop_end = do_line_logic(connectedline, absolutepath, i);
834 41571 i += skiplines;
835
836 // if a loop ended on this line, should it run again?
837
9/10
✓ Branch 0 taken 2262 times.
✓ Branch 1 taken 39309 times.
✓ Branch 2 taken 2262 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 666 times.
✓ Branch 5 taken 1596 times.
✓ Branch 6 taken 1332 times.
✓ Branch 7 taken 20433 times.
✓ Branch 8 taken 666 times.
✓ Branch 9 taken 20271 times.
41571 if (was_loop_end && whilestatus[numif].cond)
838
1/2
✓ Branch 0 taken 1332 times.
✗ Branch 1 not taken.
1332 i = whilestatus[numif].startline - 1;
839 46405 }
840
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 927 times.
939 while (in_macro_def > 0)
841 {
842
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 asar_throw_error(0, error_type_null, error_id_unclosed_macro, macro_defs[in_macro_def-1].data());
843
5/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
12 if (!pass && in_macro_def == 1) endmacro(false);
844 12 in_macro_def--;
845 12 macro_defs.remove(in_macro_def);
846 }
847
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 927 times.
927 if (numif!=startif)
848 {
849 numif=startif;
850 numtrue=startif;
851 asar_throw_error(0, error_type_null, error_id_unclosed_if);
852 }
853 927 incsrcdepth--;
854
4/4
✓ Branch 0 taken 927 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 927 times.
✓ Branch 3 taken 48 times.
10653 }
855
856 // RPG Hacker: At some point, this should probably be merged
857 // into assembleline(), since the two names just cause
858 // confusion otherwise.
859 // return value is "did a loop end on this line"
860 53443 bool do_line_logic(const char* line, const char* filename, int lineno)
861 {
862 53443 int prevnumif = numif;
863 53443 int single_line_for_tracker = 1;
864 try
865 {
866 53443 string current_line;
867
8/8
✓ Branch 0 taken 3270 times.
✓ Branch 1 taken 50173 times.
✓ Branch 2 taken 2784 times.
✓ Branch 3 taken 486 times.
✓ Branch 4 taken 168 times.
✓ Branch 5 taken 2616 times.
✓ Branch 6 taken 50341 times.
✓ Branch 7 taken 3102 times.
53443 if (numif==numtrue || (numtrue+1==numif && stribegin(line, "elseif ")))
868 {
869
1/2
✓ Branch 0 taken 50341 times.
✗ Branch 1 not taken.
50341 callstack_push cs_push(callstack_entry_type::LINE, line, lineno);
870
2/2
✓ Branch 0 taken 50239 times.
✓ Branch 1 taken 102 times.
50341 string tmp=replace_macro_args(line);
871
1/2
✓ Branch 0 taken 50239 times.
✗ Branch 1 not taken.
50239 tmp.qnormalize();
872
2/2
✓ Branch 0 taken 50207 times.
✓ Branch 1 taken 32 times.
50239 resolvedefines(current_line, tmp);
873
2/4
✓ Branch 0 taken 50207 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 50207 times.
50207 if (!confirmquotes(current_line)) asar_throw_error(0, error_type_line, error_id_mismatched_quotes);
874 50373 }
875
1/2
✓ Branch 0 taken 3102 times.
✗ Branch 1 not taken.
3102 else current_line=line;
876
877
1/2
✓ Branch 0 taken 53309 times.
✗ Branch 1 not taken.
53309 callstack_push cs_push(callstack_entry_type::LINE, current_line, lineno);
878
879
6/6
✓ Branch 0 taken 558 times.
✓ Branch 1 taken 52751 times.
✓ Branch 2 taken 522 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 522 times.
✓ Branch 5 taken 52787 times.
53309 if (stribegin(current_line, "macro ") && numif==numtrue)
880 {
881 // RPG Hacker: Slight redundancy here with code that is
882 // also in startmacro(). Could improve this for Asar 2.0.
883
1/2
✓ Branch 0 taken 522 times.
✗ Branch 1 not taken.
522 string macro_name = current_line.data()+6;
884 522 char * startpar=strqchr(macro_name.data(), '(');
885
1/2
✓ Branch 0 taken 522 times.
✗ Branch 1 not taken.
522 if (startpar) *startpar=0;
886
1/2
✓ Branch 0 taken 522 times.
✗ Branch 1 not taken.
522 macro_defs.append(macro_name);
887
888 // RPG Hacker: I think it would make more logical sense
889 // to have this ++ after the if, but hat breaks compatibility
890 // with at least one test, and it generally leads to more
891 // errors being output after a broken macro declaration.
892 522 in_macro_def++;
893
2/2
✓ Branch 0 taken 174 times.
✓ Branch 1 taken 348 times.
522 if (!pass)
894 {
895
4/4
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 140 times.
✓ Branch 3 taken 2 times.
174 if (in_macro_def == 1) startmacro(current_line.data()+6);
896
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 else tomacro(current_line);
897 }
898 522 }
899
8/8
✓ Branch 0 taken 27076 times.
✓ Branch 1 taken 25711 times.
✓ Branch 2 taken 546 times.
✓ Branch 3 taken 52241 times.
✓ Branch 4 taken 510 times.
✓ Branch 5 taken 36 times.
✓ Branch 6 taken 510 times.
✓ Branch 7 taken 52277 times.
52787 else if (!stricmp(current_line, "endmacro") && numif==numtrue)
900 {
901
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 510 times.
510 if (in_macro_def == 0) asar_throw_error(0, error_type_line, error_id_misplaced_endmacro);
902 else
903 {
904 510 in_macro_def--;
905 510 macro_defs.remove(in_macro_def);
906
2/2
✓ Branch 0 taken 170 times.
✓ Branch 1 taken 340 times.
510 if (!pass)
907 {
908
3/4
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 140 times.
✗ Branch 3 not taken.
170 if (in_macro_def == 0) endmacro(true);
909
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 else tomacro(current_line);
910 }
911 }
912 }
913
2/2
✓ Branch 0 taken 2286 times.
✓ Branch 1 taken 49991 times.
52277 else if (in_macro_def > 0)
914 {
915
3/4
✓ Branch 0 taken 762 times.
✓ Branch 1 taken 1524 times.
✓ Branch 2 taken 762 times.
✗ Branch 3 not taken.
2286 if (!pass) tomacro(current_line);
916 }
917 else
918 {
919
2/2
✓ Branch 0 taken 45159 times.
✓ Branch 1 taken 4832 times.
49991 assembleline(filename, lineno, current_line, single_line_for_tracker);
920 }
921 58277 }
922
2/2
✓ Branch 0 taken 4834 times.
✓ Branch 1 taken 134 times.
4968 catch (errline&) {}
923
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 43101 times.
43161 return (numif != prevnumif || single_line_for_tracker == 3)
924
12/14
✓ Branch 0 taken 43161 times.
✓ Branch 1 taken 5448 times.
✓ Branch 2 taken 5508 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1465 times.
✓ Branch 5 taken 4043 times.
✓ Branch 6 taken 2930 times.
✓ Branch 7 taken 1289 times.
✓ Branch 8 taken 1917 times.
✓ Branch 9 taken 1013 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 1465 times.
✓ Branch 12 taken 452 times.
✓ Branch 13 taken 1013 times.
91770 && (whilestatus[numif].iswhile || whilestatus[numif].is_for);
925 }
926
927
928 4 void parse_std_includes(const char* textfile, autoarray<string>& outarray)
929 {
930 4 char* content = readfilenative(textfile);
931
932
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (content != nullptr)
933 {
934 1 char* pos = content;
935
936
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 while (pos[0] != '\0')
937 {
938 4 string stdinclude;
939
940 do
941 {
942
3/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 3 times.
30 if (pos[0] != '\r' && pos[0] != '\n')
943 {
944
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 stdinclude += pos[0];
945 }
946 30 pos++;
947
4/4
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 3 times.
30 } while (pos[0] != '\0' && pos[0] != '\n');
948
949
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 strip_whitespace(stdinclude);
950
951
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 if (stdinclude != "")
952 {
953
3/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
3 if (!path_is_absolute(stdinclude))
954 {
955
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 stdinclude = dir(textfile) + stdinclude;
956 }
957
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 outarray.append(normalize_path(stdinclude));
958 }
959 4 }
960
961 1 free(content);
962 }
963 4 }
964
965 280 void parse_std_defines(const char* textfile)
966 {
967
968 // RPG Hacker: add built-in defines.
969 // (They're not really standard defines, but I was lazy and this was
970 // one convenient place for doing it).
971 280 builtindefines.create("assembler") = "asar";
972
6/9
✓ Branch 0 taken 277 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 124 times.
✓ Branch 3 taken 153 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 153 times.
✗ Branch 8 not taken.
280 builtindefines.create("assembler_ver") = dec(get_version_int());
973
3/6
✓ Branch 0 taken 280 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 280 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 124 times.
✗ Branch 5 not taken.
280 builtindefines.create("assembler_time") = dec(time(nullptr));
974
975
2/2
✓ Branch 0 taken 276 times.
✓ Branch 1 taken 4 times.
280 if(textfile == nullptr) return;
976
977 4 char* content = readfilenative(textfile);
978
979
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (content != nullptr)
980 {
981 1 char* pos = content;
982
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 while (*pos != 0) {
983 5 string define_name;
984 5 string define_val;
985
986
4/4
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 3 times.
29 while (*pos != '=' && *pos != '\n') {
987
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if(*pos == '\r') { pos++; continue; }
988
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 define_name += *pos;
989 24 pos++;
990 }
991
4/6
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 3 times.
5 if (*pos != 0 && *pos != '\r' && *pos != '\n') pos++; // skip =
992
3/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 5 times.
18 while (*pos != 0 && *pos != '\n') {
993
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if(*pos == '\r') { pos++; continue; }
994
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 define_val += *pos;
995 13 pos++;
996 }
997
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (*pos != 0)
998 5 pos++; // skip \n
999 // clean define_name
1000
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 strip_whitespace(define_name);
1001
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 define_name.strip_prefix('!'); // remove leading ! if present
1002
1003
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
5 if (define_name == "")
1004 {
1005
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (define_val == "")
1006 {
1007 1 continue;
1008 }
1009
1010 asar_throw_error(pass, error_type_null, error_id_stddefines_no_identifier);
1011 }
1012
1013
2/8
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
4 if (!validatedefinename(define_name)) asar_throw_error(pass, error_type_null, error_id_cmdl_define_invalid, "stddefines.txt", define_name.data());
1014
1015 // clean define_val
1016 4 const char* defval = define_val.data();
1017 4 string cleaned_defval;
1018
1019
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (*defval == 0) {
1020 // no value
1021
2/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
2 if (clidefines.exists(define_name)) asar_throw_error(pass, error_type_null, error_id_cmdl_define_override, "Std define", define_name.data());
1022
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 clidefines.create(define_name) = "";
1023 2 continue;
1024 }
1025
1026
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
4 while (*defval == ' ' || *defval == '\t') defval++; // skip whitespace in beginning
1027
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (*defval == '"') {
1028 1 defval++; // skip opening quote
1029
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
6 while (*defval != '"' && *defval != 0)
1030
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 cleaned_defval += *defval++;
1031
1032
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (*defval == 0) {
1033 asar_throw_error(pass, error_type_null, error_id_mismatched_quotes);
1034 }
1035 1 defval++; // skip closing quote
1036
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 while (*defval == ' ' || *defval == '\t') defval++; // skip whitespace
1037
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (*defval != 0 && *defval != '\n')
1038 asar_throw_error(pass, error_type_null, error_id_stddefine_after_closing_quote);
1039
1040
2/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1 if (clidefines.exists(define_name)) asar_throw_error(pass, error_type_null, error_id_cmdl_define_override, "Std define", define_name.data());
1041
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 clidefines.create(define_name) = cleaned_defval;
1042 1 continue;
1043 }
1044 else
1045 {
1046 // slightly hacky way to remove trailing whitespace
1047
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 const char* defval_end = strchr(defval, '\n'); // slightly hacky way to get end of string or newline
1048
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!defval_end) defval_end = strchr(defval, 0);
1049 1 defval_end--;
1050
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
4 while (*defval_end == ' ' || *defval_end == '\t') defval_end--;
1051
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 cleaned_defval = string(defval, (int)(defval_end - defval + 1));
1052
1053
2/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1 if (clidefines.exists(define_name)) asar_throw_error(pass, error_type_null, error_id_cmdl_define_override, "Std define", define_name.data());
1054
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 clidefines.create(define_name) = cleaned_defval;
1055 1 continue;
1056 1 }
1057
1058 5 }
1059 1 free(content);
1060 }
1061 }
1062
1063 bool checksum_fix_enabled = true;
1064 bool force_checksum_fix = false;
1065
1066 #define cfree(x) free((void*)x)
1067 140 static void clearmacro(const string & key, macrodata* & macro)
1068 {
1069 (void)key;
1070 140 freemacro(macro);
1071 140 }
1072
1073 321 static void clearfile(const string & key, sourcefile& filecontent)
1074 {
1075 (void)key;
1076 321 cfree(filecontent.data);
1077 321 cfree(filecontent.contents);
1078 321 }
1079 #undef cfree
1080
1081 843 static void adddefine(const string & key, string & value)
1082 {
1083
1/2
✓ Branch 0 taken 843 times.
✗ Branch 1 not taken.
843 if (!defines.exists(key)) defines.create(key) = value;
1084 843 }
1085
1086 2 string create_symbols_file(string format, uint32_t romCrc){
1087 2 string symbolfile;
1088
1089 2 std::vector<std::pair<unsigned int, const char*>> all_labels;
1090
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
6 labels.each([&all_labels](const string& key, snes_label& label) {
1091
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 all_labels.push_back(std::make_pair(label.pos, key.data()));
1092 4 });
1093
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::sort(all_labels.begin(), all_labels.end(),
1094 2 [](auto& l, auto& r) {
1095
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (l.first == r.first)
1096 return strcmp(l.second, r.second) < 0;
1097 2 return l.first < r.first;
1098 });
1099
1100
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 format = lower(format);
1101
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if(format == "wla")
1102 {
1103
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 symbolfile = "; wla symbolic information file\n";
1104
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 symbolfile += "; generated by asar\n";
1105
1106
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 symbolfile += "\n[labels]\n";
1107
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (auto& p : all_labels) {
1108 4 char buffer[10];
1109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 std::snprintf(buffer, sizeof(buffer), "%02X:%04X ", (p.first >> 16) & 0xFF, p.first & 0xFFFF);
1110
1111
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 symbolfile += buffer;
1112
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 symbolfile += p.second;
1113
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 symbolfile += "\n";
1114 }
1115
1116
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 symbolfile += "\n[source files]\n";
1117 2 const autoarray<AddressToLineMapping::FileInfo>& addrToLineFileList = addressToLineMapping.getFileList();
1118
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (int i = 0; i < addrToLineFileList.count; ++i)
1119 {
1120 2 char addrToFileListStr[256];
1121 2 snprintf(addrToFileListStr, 256, "%.4x %.8x %s\n",
1122 i,
1123
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 addrToLineFileList[i].fileCrc,
1124
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 addrToLineFileList[i].filename.data()
1125 );
1126
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 symbolfile += addrToFileListStr;
1127 }
1128
1129
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 symbolfile += "\n[rom checksum]\n";
1130 {
1131 2 char romCrcStr[32];
1132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 snprintf(romCrcStr, 32, "%.8x\n",
1133 romCrc
1134 );
1135
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 symbolfile += romCrcStr;
1136 }
1137
1138
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 symbolfile += "\n[addr-to-line mapping]\n";
1139 2 const autoarray<AddressToLineMapping::AddrToLineInfo>& addrToLineInfo = addressToLineMapping.getAddrToLineInfo();
1140
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (int i = 0; i < addrToLineInfo.count; ++i)
1141 {
1142 6 char addrToLineStr[32];
1143 snprintf(addrToLineStr, 32, "%.2x:%.4x %.4x:%.8x\n",
1144
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 (addrToLineInfo[i].addr & 0xFF0000) >> 16,
1145
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 addrToLineInfo[i].addr & 0xFFFF,
1146
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 addrToLineInfo[i].fileIdx & 0xFFFF,
1147
1/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6 addrToLineInfo[i].line & 0xFFFFFFFF
1148 );
1149
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 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 for (auto& p : all_labels) {
1159 char buffer[10];
1160 std::snprintf(buffer, sizeof(buffer), "%08X ", p.first & 0xFFFFFF);
1161
1162 symbolfile += buffer;
1163 symbolfile += p.second;
1164 symbolfile += "\n";
1165 }
1166 }
1167 4 return symbolfile;
1168 2 }
1169
1170
1171 2 bool in_top_level_file()
1172 {
1173 2 int num_files = 0;
1174
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (int i = callstack.count-1; i >= 0; --i)
1175 {
1176
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (callstack[i].type == callstack_entry_type::FILE)
1177 {
1178 2 num_files++;
1179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (num_files > 1) break;
1180 }
1181 }
1182 2 return (num_files <= 1);
1183 }
1184
1185 11860 const char* get_current_file_name()
1186 {
1187
2/2
✓ Branch 0 taken 43720 times.
✓ Branch 1 taken 821 times.
44541 for (int i = callstack.count-1; i >= 0; --i)
1188 {
1189
2/2
✓ Branch 0 taken 11039 times.
✓ Branch 1 taken 32681 times.
43720 if (callstack[i].type == callstack_entry_type::FILE)
1190 11039 return callstack[i].content.raw();
1191 }
1192 821 return nullptr;
1193 }
1194
1195 9023 int get_current_line()
1196 {
1197
2/2
✓ Branch 0 taken 26648 times.
✓ Branch 1 taken 11 times.
26659 for (int i = callstack.count-1; i >= 0; --i)
1198 {
1199
2/2
✓ Branch 0 taken 9012 times.
✓ Branch 1 taken 17636 times.
26648 if (callstack[i].type == callstack_entry_type::LINE) return callstack[i].lineno;
1200 }
1201 11 return -1;
1202 }
1203
1204 262 const char* get_current_block()
1205 {
1206
2/2
✓ Branch 0 taken 263 times.
✓ Branch 1 taken 11 times.
274 for (int i = callstack.count-1; i >= 0; --i)
1207 {
1208
6/6
✓ Branch 0 taken 207 times.
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 195 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 251 times.
✓ Branch 5 taken 12 times.
263 if (callstack[i].type == callstack_entry_type::LINE || callstack[i].type == callstack_entry_type::BLOCK) return callstack[i].content.raw();
1209 }
1210 11 return nullptr;
1211 }
1212
1213
1214 284 void reseteverything()
1215 {
1216 284 string str;
1217 284 labels.reset();
1218 284 defines.reset();
1219
1/2
✓ Branch 0 taken 284 times.
✗ Branch 1 not taken.
284 builtindefines.each(adddefine);
1220
1/2
✓ Branch 0 taken 284 times.
✗ Branch 1 not taken.
284 clidefines.each(adddefine);
1221 284 structs.reset();
1222
1223
1/2
✓ Branch 0 taken 284 times.
✗ Branch 1 not taken.
284 macros.each(clearmacro);
1224 284 macros.reset();
1225
1226
1/2
✓ Branch 0 taken 284 times.
✗ Branch 1 not taken.
284 filecontents.each(clearfile);
1227 284 filecontents.reset();
1228
1229 284 writtenblocks.reset();
1230
1231 284 optimizeforbank=-1;
1232 284 optimize_dp = optimize_dp_flag::ALWAYS;
1233 284 dp_base = 0;
1234 284 optimize_address = optimize_address_flag::MIRRORS;
1235
1236
1/2
✓ Branch 0 taken 284 times.
✗ Branch 1 not taken.
284 closecachedfiles();
1237
1238 284 incsrcdepth=0;
1239 284 label_counter = 0;
1240 284 errored = false;
1241 284 checksum_fix_enabled = true;
1242 284 force_checksum_fix = false;
1243
1244 284 in_macro_def = 0;
1245
1246 #ifndef ASAR_SHARED
1247 3 free(const_cast<unsigned char*>(romdata_r));
1248 #endif
1249
1250 284 callstack.reset();
1251 284 simple_callstacks = true;
1252 #undef free
1253 443 }
1254