asar coverage - build #134


src/asar/
File: src/asar/main.cpp
Date: 2024-01-23 06:23:22
Lines:
672/713
94.2%
Functions:
37/38
97.4%
Branches:
754/1105
68.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 536 int get_version_int()
47 {
48 536 return asarver_maj * 10000 + asarver_min * 100 + asarver_bug;
49 }
50
51 58 bool setmapper()
52 {
53 29 int maxscore=-99999;
54 29 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 116 int score=0;
60 116 int highbits=0;
61 116 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
3/4
✓ Branch 0 taken 1760 times.
✓ Branch 1 taken 3112 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1760 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
2/2
✓ Branch 0 taken 1848 times.
✓ Branch 1 taken 1848 times.
3696 else if (!c) foundnull=true;
73 else score-=3;
74 }
75
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 232 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
232 if (highbits>0 && highbits<=14) score-=21;//high bits set on some, but not all, bytes = unlikely to be a ROM
76
4/4
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 174 times.
✓ Branch 2 taken 87 times.
✓ Branch 3 taken 29 times.
261 if ((romdata[snestopc(0x00FFDE)]^romdata[snestopc(0x00FFDC)])!=0xFF ||
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
145 (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 29 maxscore=score;
82 29 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/8
✗ 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.
✗ Branch 6 not taken.
✗ Branch 7 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 24411 string shorten_to_relative_path(const char* base_path, const char* target_path)
103 {
104
2/2
✓ Branch 0 taken 15915 times.
✓ Branch 1 taken 8496 times.
24411 if (stribegin(target_path, base_path)) target_path += strlen(base_path);
105 24411 return target_path;
106 }
107
108 24411 string get_top_level_directory()
109 {
110 8949 string top_level_file_dir;
111
1/2
✓ Branch 0 taken 24411 times.
✗ Branch 1 not taken.
24411 for (int i = 0; i < callstack.count; ++i)
112 {
113
2/4
✓ Branch 0 taken 24411 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8949 times.
✗ Branch 3 not taken.
24411 if (callstack[i].type == callstack_entry_type::FILE)
114 {
115
3/6
✓ Branch 0 taken 24411 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8949 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8949 times.
✗ Branch 5 not taken.
24411 top_level_file_dir = dir(callstack[i].content);
116 24411 break;
117 }
118 }
119 24411 return top_level_file_dir;
120 }
121
122 24327 string generate_call_details_string(const char* current_block, const char* current_call, int indentation, bool add_lines)
123 {
124 8907 string e;
125
4/4
✓ Branch 0 taken 15483 times.
✓ Branch 1 taken 8844 times.
✓ Branch 2 taken 63 times.
✓ Branch 3 taken 24 times.
24327 if (current_block != nullptr || current_call != nullptr)
126 {
127 8883 string indent;
128
3/4
✓ Branch 0 taken 7876 times.
✓ Branch 1 taken 16403 times.
✓ Branch 2 taken 2852 times.
✗ Branch 3 not taken.
24279 if (add_lines) indent += "|";
129
4/4
✓ Branch 0 taken 97116 times.
✓ Branch 1 taken 15396 times.
✓ Branch 2 taken 35532 times.
✓ Branch 3 taken 8883 times.
121395 for (; indentation > 0; --indentation) indent += " ";
130
131
8/14
✓ Branch 0 taken 24153 times.
✓ Branch 1 taken 126 times.
✓ Branch 2 taken 8820 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8820 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8820 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 8820 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 8820 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 8820 times.
✗ Branch 13 not taken.
39612 if (current_block != nullptr) e += STR "\n"+indent+"in block: ["+current_block+"]";
132
8/14
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 24153 times.
✓ Branch 2 taken 63 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 63 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 63 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 63 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 63 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 63 times.
✗ Branch 13 not taken.
24342 if (current_call != nullptr) e += STR "\n"+indent+"in macro call: [%"+current_call+"]";
133 24279 }
134 24327 return e;
135 }
136
137 24411 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
2/4
✓ Branch 0 taken 8949 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8949 times.
✗ Branch 3 not taken.
33360 return shorten_to_relative_path(get_top_level_directory(), current_file);
145 }
146
147 8659 string generate_filename_and_line(const char* current_file, int current_line_no)
148 {
149
1/2
✓ Branch 0 taken 3245 times.
✗ Branch 1 not taken.
11904 return STR current_file
150
13/20
✓ Branch 0 taken 8623 times.
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 8623 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 8623 times.
✓ Branch 5 taken 18 times.
✓ Branch 6 taken 3227 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 18 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 3245 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 3227 times.
✓ Branch 13 taken 18 times.
✓ Branch 14 taken 3227 times.
✓ Branch 15 taken 18 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
20563 + (current_line_no>=0?STR ":"+dec(current_line_no+1):"");
151 }
152
153 7876 string format_stack_line(const printable_callstack_entry& entry, int stack_frame_index)
154 {
155
1/2
✓ Branch 0 taken 2852 times.
✗ Branch 1 not taken.
2852 string indent = "\n| ";
156
2/4
✓ Branch 0 taken 2852 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2852 times.
✗ Branch 3 not taken.
7876 indent += dec(stack_frame_index);
157
1/2
✓ Branch 0 taken 2852 times.
✗ Branch 1 not taken.
7876 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
3/4
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 7628 times.
✓ Branch 2 taken 124 times.
✗ Branch 3 not taken.
7876 if (stack_frame_index < 100) indent += " ";
161
3/4
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 2908 times.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
2976 if (stack_frame_index < 10) indent += " ";
162 return indent
163
2/4
✓ Branch 0 taken 2852 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2852 times.
✗ Branch 3 not taken.
15752 + generate_filename_and_line(entry.prettypath, entry.lineno)
164
1/2
✓ Branch 0 taken 2852 times.
✗ Branch 1 not taken.
13580 + entry.details;
165 7876 }
166
167 23628 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 8556 printable_callstack_entry new_entry;
170
1/2
✓ Branch 0 taken 8556 times.
✗ Branch 1 not taken.
8556 new_entry.fullpath = current_file;
171
2/4
✓ Branch 0 taken 23628 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8556 times.
✗ Branch 3 not taken.
23628 new_entry.prettypath = get_pretty_filename(current_file);
172 23628 new_entry.lineno = current_line_no;
173
2/4
✓ Branch 0 taken 8556 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8556 times.
✗ Branch 3 not taken.
38700 new_entry.details = generate_call_details_string(current_block, current_call, indentation, add_lines).raw();
174
1/2
✓ Branch 0 taken 8556 times.
✗ Branch 1 not taken.
23628 out->append(new_entry);
175 23628 }
176
177 700 void get_current_line_details(string* location, string* details, bool exclude_block)
178 {
179 352 const char* current_file = nullptr;
180 352 const char* current_block = nullptr;
181 352 const char* current_call = nullptr;
182 352 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 351 case callstack_entry_type::FILE:
188 351 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
3/6
✓ Branch 0 taken 351 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 351 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 351 times.
✗ Branch 5 not taken.
699 *location = generate_filename_and_line(get_pretty_filename(current_file), current_line_no);
191
2/4
✓ Branch 0 taken 351 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 351 times.
✗ Branch 3 not taken.
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
3/4
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 495 times.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
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.
993 if (current_line_no == -1) current_line_no = callstack[i].lineno;
199 333 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 492 break;
203 }
204 }
205 1 *location = "";
206 1 *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 352 const char* current_file = nullptr;
213 352 const char* current_block = nullptr;
214 352 const char* current_call = nullptr;
215 352 int current_line_no = -1;
216
2/2
✓ Branch 0 taken 96978 times.
✓ Branch 1 taken 700 times.
97678 for (int i = 0; i < callstack.count; ++i)
217 {
218
4/5
✓ Branch 0 taken 24327 times.
✓ Branch 1 taken 126 times.
✓ Branch 2 taken 24291 times.
✓ Branch 3 taken 48234 times.
✗ Branch 4 not taken.
96978 switch (callstack[i].type)
219 {
220 24327 case callstack_entry_type::FILE:
221
2/2
✓ Branch 0 taken 23628 times.
✓ Branch 1 taken 699 times.
24327 if (current_file != nullptr)
222 {
223 23628 push_stack_line(out, current_file, current_block, current_call, current_line_no, indentation, add_lines);
224 }
225 8907 current_file = callstack[i].content;
226 8907 current_block = nullptr;
227 8907 current_call = nullptr;
228 8907 current_line_no = -1;
229 24327 break;
230 126 case callstack_entry_type::MACRO_CALL:
231 63 current_block = nullptr;
232 63 current_call = callstack[i].content;
233 126 break;
234 8889 case callstack_entry_type::LINE:
235 24291 current_line_no = callstack[i].lineno;
236 8889 current_block = callstack[i].content;
237 24291 break;
238 17604 case callstack_entry_type::BLOCK:
239 17604 current_block = callstack[i].content;
240 48234 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
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
50 e += "\nFull call stack:";
254
2/2
✓ Branch 0 taken 7876 times.
✓ Branch 1 taken 50 times.
7926 for (int i = printable_stack.count-1; i >= 0; --i)
255 {
256
3/6
✓ Branch 0 taken 2852 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2852 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2852 times.
✗ Branch 5 not taken.
7876 e += format_stack_line(printable_stack[i], i);
257 }
258 }
259 348 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 236 const char* current_call = nullptr;
268
2/2
✓ Branch 0 taken 64320 times.
✓ Branch 1 taken 384 times.
64704 for (i = callstack.count-1; i >= 0 ; --i)
269 {
270
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 64236 times.
64320 if (callstack[i].type == callstack_entry_type::MACRO_CALL)
271 {
272 42 current_call = callstack[i].content;
273 84 break;
274 }
275 }
276
277 236 const char* current_file = nullptr;
278 236 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 42 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/5
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
✓ Branch 3 taken 168 times.
✗ Branch 4 not taken.
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 42 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.
126 if (current_line_no == -1) current_line_no = callstack[j].lineno;
299 42 break;
300 84 case callstack_entry_type::BLOCK:
301 84 break;
302 }
303
304
3/4
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 168 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
336 if (current_file != nullptr && current_line_no != -1) stop = true;
305
306
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 126 times.
294 if (stop) break;
307 }
308 }
309
310 236 string e;
311
3/4
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 384 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
468 if (current_call != nullptr && current_file != nullptr)
312 {
313
4/8
✓ Branch 0 taken 84 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.
168 e += STR "\n called from: " + generate_filename_and_line(get_pretty_filename(current_file), current_line_no)
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.
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 27 times.
54 asar_error_id vfile_error_to_error_id(virtual_file_error vfile_error)
328 {
329
2/5
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
27 switch (vfile_error)
330 {
331 9 case vfe_doesnt_exist:
332 9 return error_id_file_not_found;
333 case vfe_access_denied:
334 return error_id_failed_to_open_file_access_denied;
335 18 case vfe_not_regular_file:
336 18 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 1828 static int getlenforlabel(snes_label thislabel, bool exists)
358 {
359 1828 unsigned int bank = thislabel.pos>>16;
360 1828 unsigned int word = thislabel.pos&0xFFFF;
361 916 bool lblfreespace = thislabel.freespace_id > 0;
362 unsigned int relaxed_bank;
363
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 1774 times.
1828 if(optimizeforbank >= 0) {
364 54 relaxed_bank = optimizeforbank;
365 } else {
366
2/2
✓ Branch 0 taken 1360 times.
✓ Branch 1 taken 414 times.
1774 if(freespaceid == 0) {
367 1360 relaxed_bank = snespos >> 16;
368 } else {
369 414 int target_bank = freespaces[freespaceid].bank;
370
2/2
✓ Branch 0 taken 207 times.
✓ Branch 1 taken 207 times.
414 if(target_bank == -2) relaxed_bank = 0;
371
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 72 times.
144 else if(target_bank == -1) relaxed_bank = 0x40;
372 else relaxed_bank = target_bank;
373 }
374 }
375
2/2
✓ Branch 0 taken 912 times.
✓ Branch 1 taken 916 times.
1828 if (!exists)
376 {
377 141 return 2;
378 }
379
6/6
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 1510 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 9 times.
✓ Branch 5 taken 9 times.
1546 else if((optimize_dp == optimize_dp_flag::RAM) && bank == 0x7E && (word-dp_base < 0x100))
380 {
381 9 return 1;
382 }
383
6/8
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 1450 times.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 78 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 39 times.
✓ Branch 7 taken 39 times.
1528 else if(optimize_dp == optimize_dp_flag::ALWAYS && (bank == 0x7E || !(bank & 0x40)) && (word-dp_base < 0x100))
384 {
385 27 return 1;
386 }
387 1474 else if (
388 // if we should optimize ram accesses...
389
4/4
✓ Branch 0 taken 853 times.
✓ Branch 1 taken 621 times.
✓ Branch 2 taken 114 times.
✓ Branch 3 taken 571 times.
1474 (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 264 times.
✓ Branch 1 taken 72 times.
336 && !(relaxed_bank & 0x40)
392 // and the label is in low RAM
393
5/6
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 210 times.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 27 times.
✓ Branch 4 taken 27 times.
✗ Branch 5 not taken.
264 && bank == 0x7E && word < 0x2000 && !lblfreespace)
394 {
395 27 return 2;
396 }
397
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 603 times.
1420 else if (
398 // if we should optimize mirrors...
399
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 607 times.
712 optimize_address == optimize_address_flag::MIRRORS
400 // we're in a bank with ram mirrors...
401
2/2
✓ Branch 0 taken 156 times.
✓ Branch 1 taken 54 times.
210 && !(relaxed_bank & 0x40)
402 // and the label is in a mirrored section
403
5/6
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 81 times.
✓ Branch 3 taken 75 times.
✓ Branch 4 taken 27 times.
✓ Branch 5 taken 3 times.
156 && !(bank & 0x40) && word < 0x8000 && !lblfreespace)
404 {
405 27 return 2;
406 }
407
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 1312 times.
1366 else if (optimizeforbank>=0)
408 {
409 // if optimizing for a specific bank:
410 // if the label is in freespace, never optimize
411
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 27 times.
54 if (thislabel.freespace_id > 0) return 3;
412
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 27 times.
54 else if (bank==(unsigned int)optimizeforbank) return 2;
413 36 else return 3;
414 }
415
4/4
✓ Branch 0 taken 1150 times.
✓ Branch 1 taken 162 times.
✓ Branch 2 taken 162 times.
✓ Branch 3 taken 988 times.
1312 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 162 times.
✓ Branch 1 taken 162 times.
324 if (thislabel.freespace_id != freespaceid) return 3;
420 96 else return 2;
421 }
422
2/2
✓ Branch 0 taken 492 times.
✓ Branch 1 taken 496 times.
988 else if ((int)bank != snespos >> 16){ return 3; }
423 910 else { return 2;}
424 }
425
426
427 609 bool is_hex_constant(const char* str){
428
2/2
✓ Branch 0 taken 522 times.
✓ Branch 1 taken 87 times.
609 if (*str=='$')
429 {
430 522 str++;
431
2/2
✓ Branch 0 taken 1512 times.
✓ Branch 1 taken 522 times.
2034 while(is_xdigit(*str)) {
432 1512 str++;
433 }
434
2/2
✓ Branch 0 taken 261 times.
✓ Branch 1 taken 261 times.
522 if(*str=='\0'){
435 261 return true;
436 }
437 }
438 45 return false;
439 }
440
441 8745 int getlen(const char * orgstr, bool optimizebankextraction)
442 {
443 8745 const char * str=orgstr;
444 8745 freespaced=false;
445
446 8745 const char* posneglabel = str;
447
1/2
✓ Branch 0 taken 4377 times.
✗ Branch 1 not taken.
8745 string posnegname = posneglabelname(&posneglabel, false);
448
449
3/4
✓ Branch 0 taken 8745 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 4371 times.
8745 if (posnegname.length() > 0)
450 {
451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (*posneglabel != '\0') goto notposneglabel;
452
453
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
10 if (!pass) return 2;
454 4 snes_label label_data;
455 // RPG Hacker: Umm... what kind of magic constant is this?
456 4 label_data.pos = 31415926;
457
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 bool found = labelval(posnegname, &label_data);
458
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 return getlenforlabel(label_data, found);
459 }
460 8739 notposneglabel:
461 4371 int len=0;
462
2/2
✓ Branch 0 taken 9159 times.
✓ Branch 1 taken 8739 times.
17898 while (*str)
463 {
464 4581 int thislen=0;
465 9159 bool maybebankextraction=(str==orgstr);
466
2/2
✓ Branch 0 taken 7002 times.
✓ Branch 1 taken 2157 times.
9159 if (*str=='$')
467 {
468 7002 str++;
469 int i;
470
2/2
✓ Branch 0 taken 19008 times.
✓ Branch 1 taken 7002 times.
26010 for (i=0;is_xdigit(str[i]);i++);
471 //if (i&1) warn(S dec(i)+"-digit hex value");//blocked in getnum instead
472 7002 thislen=(i+1)/2;
473 7002 str+=i;
474 }
475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2157 times.
2157 else if (*str=='%')
476 {
477 str++;
478 int i;
479 for (i=0;str[i]=='0' || str[i]=='1';i++);
480 //if (i&7) warn(S dec(i)+"-digit binary value");
481 thislen=(i+7)/8;
482 str+=i;
483 }
484
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2157 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2157 else if (str[0]=='\'' && str[2]=='\'')
485 {
486 thislen=1;
487 str+=3;
488 }
489
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 2124 times.
2157 else if (is_digit(*str))
490 {
491 33 int val=strtol(str, const_cast<char**>(&str), 10);
492
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (val>=0) thislen=1;
493
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 18 times.
33 if (val>=256) thislen=2;
494
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (val>=65536) thislen=3;
495 }
496
7/8
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 1824 times.
✓ Branch 2 taken 300 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 150 times.
✓ Branch 5 taken 150 times.
✓ Branch 6 taken 912 times.
✓ Branch 7 taken 150 times.
2124 else if (is_ualpha(*str) || *str=='.' || *str=='?')
497 {
498 912 snes_label thislabel;
499
1/2
✓ Branch 0 taken 1824 times.
✗ Branch 1 not taken.
1824 bool exists=labelval(&str, &thislabel);
500
1/2
✓ Branch 0 taken 912 times.
✗ Branch 1 not taken.
1824 thislen=getlenforlabel(thislabel, exists);
501 }
502 300 else str++;
503
4/4
✓ Branch 0 taken 1359 times.
✓ Branch 1 taken 7800 times.
✓ Branch 2 taken 576 times.
✓ Branch 3 taken 210 times.
9159 if (optimizebankextraction && maybebankextraction &&
504
4/6
✓ Branch 0 taken 1149 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1149 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 573 times.
✓ Branch 5 taken 576 times.
1149 (!strcmp(str, ">>16") || !strcmp(str, "/65536") || !strcmp(str, "/$10000")))
505 return 1;
506
2/2
✓ Branch 0 taken 4395 times.
✓ Branch 1 taken 186 times.
4581 if (thislen>len) len=thislen;
507 }
508 4371 return len;
509 8745 }
510
511 struct strcompare {
512 bool operator() (const char * lhs, const char * rhs) const
513 {
514 return strcmp(lhs, rhs)<0;
515 }
516 };
517
518 struct stricompare {
519 bool operator() (const char * lhs, const char * rhs) const
520 {
521 return stricmp(lhs, rhs)<0;
522 }
523 };
524
525 struct sourcefile {
526 char *data;
527 char** contents;
528 int numlines;
529 };
530
531 static assocarr<sourcefile> filecontents;
532 assocarr<string> defines;
533 // needs to be separate because defines is reset between parsing arguments and patching
534 assocarr<string> clidefines;
535 assocarr<string> builtindefines;
536
537 5066 bool validatedefinename(const char * name)
538 {
539
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5066 times.
5066 if (!name[0]) return false;
540
2/2
✓ Branch 0 taken 52624 times.
✓ Branch 1 taken 5066 times.
57690 for (int i = 0;name[i];i++)
541 {
542
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52624 times.
52624 if (!is_ualnum(name[i])) return false;
543 }
544
545 2536 return true;
546 }
547
548 185752 void resolvedefines(string& out, const char * start)
549 {
550
2/2
✓ Branch 0 taken 89708 times.
✓ Branch 1 taken 3 times.
89711 recurseblock rec;
551 89708 const char * here=start;
552
2/2
✓ Branch 0 taken 154586 times.
✓ Branch 1 taken 31160 times.
185746 if (!strchr(here, '!'))
553 {
554
1/2
✓ Branch 0 taken 74116 times.
✗ Branch 1 not taken.
154586 out += here;
555 74116 return;
556 }
557
2/2
✓ Branch 0 taken 212600 times.
✓ Branch 1 taken 31070 times.
243670 while (*here)
558 {
559
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] == '\\')
560 {
561 // allow using \\ as escape sequence
562
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
18 if (in_macro_def > 0) out += "\\";
563
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
18 out += "\\";
564 18 here += 2;
565 }
566
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] == '!')
567 {
568 // allow using \! to escape !
569
3/4
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 90 times.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
126 if (in_macro_def > 0) out += "\\";
570
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
126 out+="!";
571 126 here += 2;
572 }
573
2/2
✓ Branch 0 taken 33422 times.
✓ Branch 1 taken 179034 times.
212456 else if (*here=='!')
574 {
575
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
576 16729 string defname;
577 33422 here++;
578
579 16729 int depth = 0;
580
2/2
✓ Branch 0 taken 342 times.
✓ Branch 1 taken 33422 times.
33764 for (const char* depth_str = here; *depth_str=='^'; depth_str++)
581 {
582 342 depth++;
583 }
584 33422 here += depth;
585
586
2/2
✓ Branch 0 taken 3960 times.
✓ Branch 1 taken 29462 times.
33422 if (depth != in_macro_def)
587 {
588
1/2
✓ Branch 0 taken 1980 times.
✗ Branch 1 not taken.
3960 out += '!';
589
4/4
✓ Branch 0 taken 198 times.
✓ Branch 1 taken 1980 times.
✓ Branch 2 taken 99 times.
✓ Branch 3 taken 1980 times.
4158 for (int i=0; i < depth; ++i) out += '^';
590
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);
591 1953 continue;
592 3906 }
593
594
2/2
✓ Branch 0 taken 324 times.
✓ Branch 1 taken 29138 times.
29462 if (*here=='{')
595 {
596 324 here++;
597 162 string unprocessedname;
598 162 int braces=1;
599 while (true)
600 {
601
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 3240 times.
3456 if (*here=='{') braces++;
602
2/2
✓ Branch 0 taken 540 times.
✓ Branch 1 taken 2916 times.
3456 if (*here=='}') braces--;
603
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);
604
2/2
✓ Branch 0 taken 1728 times.
✓ Branch 1 taken 1728 times.
3456 if (!braces) break;
605
1/2
✓ Branch 0 taken 1566 times.
✗ Branch 1 not taken.
3132 unprocessedname+=*here++;
606 }
607
1/2
✓ Branch 0 taken 162 times.
✗ Branch 1 not taken.
324 here++;
608
1/2
✓ Branch 0 taken 324 times.
✗ Branch 1 not taken.
324 resolvedefines(defname, unprocessedname);
609
3/7
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 216 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 108 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
324 if (!validatedefinename(defname)) asar_throw_error(0, error_type_line, error_id_invalid_define_name);
610 324 }
611 else
612 {
613
4/4
✓ Branch 0 taken 149773 times.
✓ Branch 1 taken 14551 times.
✓ Branch 2 taken 74969 times.
✓ Branch 3 taken 14587 times.
178911 while (is_ualnum(*here)) defname+=*here++;
614 }
615
616
2/2
✓ Branch 0 taken 15665 times.
✓ Branch 1 taken 13797 times.
29462 if (first)
617 {
618 enum {
619 null,
620 append,
621 expand,
622 domath,
623 setifnotset,
624 } mode;
625 if(0);
626
2/2
✓ Branch 0 taken 1500 times.
✓ Branch 1 taken 14165 times.
15665 else if (stribegin(here, " = ")) { here+=3; mode=null; }
627
2/2
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 13859 times.
14165 else if (stribegin(here, " += ")) { here+=4; mode=append; }
628
2/2
✓ Branch 0 taken 162 times.
✓ Branch 1 taken 13697 times.
13859 else if (stribegin(here, " := ")) { here+=4; mode=expand; }
629
2/2
✓ Branch 0 taken 5778 times.
✓ Branch 1 taken 7919 times.
13697 else if (stribegin(here, " #= ")) { here+=4; mode=domath; }
630
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7907 times.
7919 else if (stribegin(here, " ?= ")) { here+=4; mode=setifnotset; }
631 7907 else goto notdefineset;
632 3882 string val;
633
2/2
✓ Branch 0 taken 507 times.
✓ Branch 1 taken 7251 times.
7758 if (*here=='"')
634 {
635 507 here++;
636 while (true)
637 {
638
2/2
✓ Branch 0 taken 1290 times.
✓ Branch 1 taken 1287 times.
2577 if (*here=='"')
639 {
640
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;
641
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 else if (here[1]=='"') here++;
642 else asar_throw_error(0, error_type_line, error_id_broken_define_declaration);
643 }
644
1/2
✓ Branch 0 taken 1035 times.
✗ Branch 1 not taken.
2070 val+=*here++;
645 }
646 507 here++;
647 }
648 else
649 {
650
5/6
✓ Branch 0 taken 49863 times.
✓ Branch 1 taken 3624 times.
✓ Branch 2 taken 49863 times.
✓ Branch 3 taken 3627 times.
✓ Branch 4 taken 24936 times.
✗ Branch 5 not taken.
57114 while (*here && *here!=' ') val+=*here++;
651 }
652 //if (strqchr(val.data(), ';')) *strqchr(val.data(), ';')=0;
653
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 7758 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3882 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
7758 if (*here && !stribegin(here, " : ")) asar_throw_error(0, error_type_line, error_id_broken_define_declaration);
654 // RPG Hacker: Is it really a good idea to normalize
655 // the content of defines? That kinda violates their
656 // functionality as a string replacement mechanism.
657
1/2
✓ Branch 0 taken 7758 times.
✗ Branch 1 not taken.
7758 val.qnormalize();
658
659 // RPG Hacker: throw an error if we're trying to overwrite built-in defines.
660
4/4
✓ Branch 0 taken 3891 times.
✓ Branch 1 taken 3867 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 3873 times.
7758 if (builtindefines.exists(defname))
661 {
662
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());
663 }
664
665
5/6
✓ 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.
✗ Branch 5 not taken.
7740 switch (mode)
666 {
667 744 case null:
668 {
669
2/4
✓ Branch 0 taken 1482 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 744 times.
✗ Branch 3 not taken.
1482 defines.create(defname) = val;
670 744 break;
671 }
672 153 case append:
673 {
674
3/6
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 153 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 153 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
306 if (!defines.exists(defname)) asar_throw_error(0, error_type_line, error_id_define_not_found, defname.data());
675
2/4
✓ Branch 0 taken 153 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 153 times.
✗ Branch 3 not taken.
306 string oldval = defines.find(defname);
676
2/4
✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 153 times.
✗ Branch 3 not taken.
306 val=oldval+val;
677
2/4
✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 153 times.
✗ Branch 3 not taken.
306 defines.create(defname) = val;
678 153 break;
679 306 }
680 81 case expand:
681 {
682 81 string newval;
683
1/2
✓ Branch 0 taken 162 times.
✗ Branch 1 not taken.
162 resolvedefines(newval, val);
684
2/4
✓ Branch 0 taken 162 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 81 times.
✗ Branch 3 not taken.
162 defines.create(defname) = newval;
685 81 break;
686 162 }
687 2889 case domath:
688 {
689 2889 string newval;
690
1/2
✓ Branch 0 taken 5778 times.
✗ Branch 1 not taken.
5778 resolvedefines(newval, val);
691
1/2
✓ Branch 0 taken 5778 times.
✗ Branch 1 not taken.
5778 double num= getnumdouble(newval);
692
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);
693
3/6
✓ Branch 0 taken 5760 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2880 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2880 times.
✗ Branch 5 not taken.
5760 defines.create(defname) = ftostr(num);
694 2880 break;
695 5778 }
696 6 case setifnotset:
697 {
698
4/8
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
12 if (!defines.exists(defname)) defines.create(defname) = val;
699 6 break;
700 }
701 }
702 7758 }
703 else
704 {
705 13797 notdefineset:
706
5/6
✓ Branch 0 taken 10912 times.
✓ Branch 1 taken 10792 times.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 10819 times.
✓ Branch 4 taken 48 times.
✗ Branch 5 not taken.
21704 if (!defname) out+="!";
707 else
708 {
709
3/6
✓ Branch 0 taken 10819 times.
✓ Branch 1 taken 10792 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10819 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
21611 if (!defines.exists(defname)) asar_throw_error(0, error_type_line, error_id_define_not_found, defname.data());
710 else {
711
2/4
✓ Branch 0 taken 10819 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10819 times.
✗ Branch 3 not taken.
21611 string thisone = defines.find(defname);
712
1/2
✓ Branch 0 taken 21611 times.
✗ Branch 1 not taken.
21611 resolvedefines(out, thisone);
713 21611 }
714 }
715 }
716
2/2
✓ Branch 0 taken 14731 times.
✓ Branch 1 taken 1953 times.
33422 }
717
1/2
✓ Branch 0 taken 89649 times.
✗ Branch 1 not taken.
179034 else out+=*here++;
718 }
719
5/8
✓ 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.
✓ Branch 6 taken 9 times.
✗ Branch 7 not taken.
31070 if (!confirmquotes(out)) { asar_throw_error(0, error_type_null, error_id_mismatched_quotes); out = ""; }
720
2/2
✓ Branch 0 taken 15547 times.
✓ Branch 1 taken 74116 times.
185746 }
721
722 bool moreonline;
723 bool asarverallowed = false;
724
725 152919 void assembleline(const char * fname, int linenum, const char * line, int& single_line_for_tracker)
726 {
727
1/2
✓ Branch 0 taken 73281 times.
✗ Branch 1 not taken.
73281 recurseblock rec;
728 152919 bool moreonlinetmp=moreonline;
729 // randomdude999: redundant, assemblefile already converted the path to absolute
730 //string absolutepath = filesystem->create_absolute_path("", fname);
731
1/2
✓ Branch 0 taken 73281 times.
✗ Branch 1 not taken.
73281 string absolutepath = fname;
732 152919 single_line_for_tracker = 1;
733 try
734 {
735
1/2
✓ Branch 0 taken 73281 times.
✗ Branch 1 not taken.
73281 string out=line;
736
1/2
✓ Branch 0 taken 152919 times.
✗ Branch 1 not taken.
152919 out.qreplace(": :", ": :");
737
1/2
✓ Branch 0 taken 152919 times.
✗ Branch 1 not taken.
152919 autoptr<char**> blocks=qsplitstr(out.temp_raw(), " : ");
738 152919 moreonline=true;
739
2/2
✓ Branch 0 taken 158148 times.
✓ Branch 1 taken 129417 times.
287565 for (int block=0;moreonline;block++)
740 {
741 158148 moreonline=(blocks[block+1] != nullptr);
742 try
743 {
744
1/2
✓ Branch 0 taken 75918 times.
✗ Branch 1 not taken.
158148 string stripped_block = strip_whitespace(blocks[block]);
745
746
1/2
✓ Branch 0 taken 75918 times.
✗ Branch 1 not taken.
75918 callstack_push cs_push(callstack_entry_type::BLOCK, stripped_block);
747
748
2/2
✓ Branch 0 taken 133740 times.
✓ Branch 1 taken 24408 times.
158148 assembleblock(stripped_block, single_line_for_tracker);
749
2/2
✓ Branch 0 taken 133728 times.
✓ Branch 1 taken 12 times.
133740 checkbankcross();
750 182568 }
751
2/2
✓ Branch 0 taken 23502 times.
✓ Branch 1 taken 918 times.
24420 catch (errblock&) {}
752
2/2
✓ Branch 0 taken 88179 times.
✓ Branch 1 taken 46467 times.
134646 if (blocks[block][0]!='\0') asarverallowed=false;
753
2/2
✓ Branch 0 taken 128427 times.
✓ Branch 1 taken 6219 times.
134646 if(single_line_for_tracker == 1) single_line_for_tracker = 0;
754 }
755 176421 }
756
1/2
✓ Branch 0 taken 23502 times.
✗ Branch 1 not taken.
23502 catch (errline&) {}
757 129417 moreonline=moreonlinetmp;
758 176421 }
759
760 int incsrcdepth=0;
761
762 // Returns true if a file is protected via
763 // an "includeonce".
764 26154 bool file_included_once(const char* file)
765 {
766
2/2
✓ Branch 0 taken 720 times.
✓ Branch 1 taken 26028 times.
26748 for (int i = 0; i < includeonce.count; ++i)
767 {
768
2/2
✓ Branch 0 taken 360 times.
✓ Branch 1 taken 360 times.
720 if (includeonce[i] == file)
769 {
770 63 return true;
771 }
772 }
773
774 9795 return false;
775 }
776
777 autoarray<string> macro_defs;
778 int in_macro_def=0;
779
780 26064 void assemblefile(const char * filename)
781 {
782 26064 incsrcdepth++;
783
2/4
✓ Branch 0 taken 9813 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9813 times.
✗ Branch 3 not taken.
26064 string absolutepath = filesystem->create_absolute_path(get_current_file_name(), filename);
784
785
4/4
✓ Branch 0 taken 26001 times.
✓ Branch 1 taken 63 times.
✓ Branch 2 taken 63 times.
✓ Branch 3 taken 9750 times.
26064 if (file_included_once(absolutepath))
786 {
787 63 return;
788 }
789
790
1/2
✓ Branch 0 taken 9750 times.
✗ Branch 1 not taken.
9750 callstack_push cs_push(callstack_entry_type::FILE, absolutepath);
791
792 sourcefile file;
793 25938 file.contents = nullptr;
794 25938 file.numlines = 0;
795
2/2
✓ Branch 0 taken 418 times.
✓ Branch 1 taken 15770 times.
25938 int startif=numif;
796
4/4
✓ Branch 0 taken 10168 times.
✓ Branch 1 taken 15770 times.
✓ Branch 2 taken 444 times.
✓ Branch 3 taken 9306 times.
25938 if (!filecontents.exists(absolutepath))
797 {
798
2/2
✓ Branch 0 taken 856 times.
✓ Branch 1 taken 6 times.
862 char * temp = readfile(absolutepath, "");
799
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 838 times.
856 if (!temp)
800 {
801
4/7
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
✗ Branch 6 not taken.
18 asar_throw_error(0, error_type_null, vfile_error_to_error_id(asar_get_last_io_error()), filename);
802
803 18 return;
804 }
805
1/2
✓ Branch 0 taken 838 times.
✗ Branch 1 not taken.
838 sourcefile& newfile = filecontents.create(absolutepath);
806
1/2
✓ Branch 0 taken 838 times.
✗ Branch 1 not taken.
838 newfile.contents =split(temp, '\n');
807 838 newfile.data = temp;
808 432 bool in_block_comment = false;
809 432 int block_comment_start = -1;
810 432 string block_comment_start_line;
811
2/2
✓ Branch 0 taken 33193 times.
✓ Branch 1 taken 838 times.
34031 for (int i=0;newfile.contents[i];i++)
812 {
813 33193 newfile.numlines++;
814 16623 char * line= newfile.contents[i];
815
2/2
✓ Branch 0 taken 33151 times.
✓ Branch 1 taken 42 times.
33193 if(in_block_comment) {
816 42 char * end = strstr(line, "]]");
817
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 6 times.
42 if(!end) {
818 36 *line = 0;
819 36 continue;
820 }
821 6 line = end+2;
822 3 in_block_comment = false;
823 }
824 33157 redo_line:
825 33169 char * comment = strqchr(line, ';');
826
2/2
✓ Branch 0 taken 8742 times.
✓ Branch 1 taken 24427 times.
33169 if(comment) {
827
3/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 8718 times.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
8742 if(comment[1] == '[' && comment[2] == '[') {
828 12 in_block_comment = true;
829 12 block_comment_start = i;
830
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 block_comment_start_line = line;
831 // this is a little messy because a multiline comment could
832 // end right on the line where it started. so if we find
833 // the end, cut the comment out of the line and recheck for
834 // more comments.
835 24 char * end = strstr(line, "]]");
836
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
24 if(end) {
837 12 memmove(comment, end+2, strlen(end+2)+1);
838 6 in_block_comment = false;
839 12 goto redo_line;
840 }
841 }
842 8730 *comment = 0;
843 }
844
5/8
✓ Branch 0 taken 33157 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 33145 times.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
33163 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'; }
845 33157 newfile.contents[i] = strip_whitespace(line);
846 }
847
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 832 times.
838 if(in_block_comment) {
848
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 callstack_push cs_push(callstack_entry_type::LINE, block_comment_start_line, block_comment_start);
849
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 asar_throw_error(0, error_type_null, error_id_unclosed_block_comment);
850 6 }
851
2/2
✓ Branch 0 taken 33193 times.
✓ Branch 1 taken 838 times.
34031 for(int i=0;newfile.contents[i];i++)
852 {
853 16623 char* line = newfile.contents[i];
854
2/2
✓ Branch 0 taken 12332 times.
✓ Branch 1 taken 20861 times.
33193 if(!*line) continue;
855
3/4
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 20861 times.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
20891 for (int j=1;line[strlen(line) - 1] == ',' && newfile.contents[i+j];j++)
856 {
857 // not using strcat because the source and dest overlap here
858 15 char* otherline = newfile.contents[i+j];
859 30 char* line_end = line + strlen(line);
860
2/2
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 30 times.
414 while(*otherline) *line_end++ = *otherline++;
861 30 *line_end = '\0';
862 static char nullstr[]="";
863 30 newfile.contents[i+j]=nullstr;
864 }
865 }
866 838 file = newfile;
867 838 } else { // filecontents.exists(absolutepath)
868
1/2
✓ Branch 0 taken 9306 times.
✗ Branch 1 not taken.
25076 file = filecontents.find(absolutepath);
869 }
870 25914 asarverallowed=true;
871
3/4
✓ Branch 0 taken 142149 times.
✓ Branch 1 taken 2406 times.
✓ Branch 2 taken 142149 times.
✗ Branch 3 not taken.
144555 for (int i=0;file.contents[i] && i<file.numlines;i++)
872 {
873 67896 string connectedline;
874
1/2
✓ Branch 0 taken 67896 times.
✗ Branch 1 not taken.
142149 int skiplines = getconnectedlines<char**>(file.contents, i, connectedline);
875
876
2/2
✓ Branch 0 taken 118641 times.
✓ Branch 1 taken 23508 times.
142149 bool was_loop_end = do_line_logic(connectedline, absolutepath, i);
877 118641 i += skiplines;
878
879 // if a loop ended on this line, should it run again?
880
8/8
✓ Branch 0 taken 6306 times.
✓ Branch 1 taken 112335 times.
✓ Branch 2 taken 4308 times.
✓ Branch 3 taken 1998 times.
✓ Branch 4 taken 1998 times.
✓ Branch 5 taken 1155 times.
✓ Branch 6 taken 1998 times.
✓ Branch 7 taken 57402 times.
118641 if (was_loop_end && whilestatus[numif].cond)
881
1/2
✓ Branch 0 taken 1998 times.
✗ Branch 1 not taken.
3996 i = whilestatus[numif].startline - 1;
882 142149 }
883
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 2406 times.
2442 while (in_macro_def > 0)
884 {
885
2/4
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
36 asar_throw_error(0, error_type_null, error_id_unclosed_macro, macro_defs[in_macro_def-1].data());
886
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);
887 36 in_macro_def--;
888
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
36 macro_defs.remove(in_macro_def);
889 }
890
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2406 times.
2406 if (numif!=startif)
891 {
892 numif=startif;
893 numtrue=startif;
894 asar_throw_error(0, error_type_null, error_id_unclosed_if);
895 }
896 2406 incsrcdepth--;
897
4/4
✓ Branch 0 taken 1242 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 1242 times.
✓ Branch 3 taken 72 times.
49587 }
898
899 // RPG Hacker: At some point, this should probably be merged
900 // into assembleline(), since the two names just cause
901 // confusion otherwise.
902 // return value is "did a loop end on this line"
903 163221 bool do_line_logic(const char* line, const char* filename, int lineno)
904 {
905 163221 int prevnumif = numif;
906 163221 int single_line_for_tracker = 1;
907 try
908 {
909 78432 string current_line;
910
8/8
✓ Branch 0 taken 9558 times.
✓ Branch 1 taken 153663 times.
✓ Branch 2 taken 8100 times.
✓ Branch 3 taken 1458 times.
✓ Branch 4 taken 504 times.
✓ Branch 5 taken 7596 times.
✓ Branch 6 taken 73905 times.
✓ Branch 7 taken 4527 times.
163221 if (numif==numtrue || (numtrue+1==numif && stribegin(line, "elseif ")))
911 {
912
1/2
✓ Branch 0 taken 73905 times.
✗ Branch 1 not taken.
73905 callstack_push cs_push(callstack_entry_type::LINE, line, lineno);
913
2/2
✓ Branch 0 taken 153861 times.
✓ Branch 1 taken 306 times.
154167 string tmp=replace_macro_args(line);
914
1/2
✓ Branch 0 taken 153861 times.
✗ Branch 1 not taken.
153861 tmp.qnormalize();
915
2/2
✓ Branch 0 taken 153765 times.
✓ Branch 1 taken 96 times.
153861 resolvedefines(current_line, tmp);
916 154263 }
917
1/2
✓ Branch 0 taken 4527 times.
✗ Branch 1 not taken.
4527 else current_line=line;
918
919
1/2
✓ Branch 0 taken 78231 times.
✗ Branch 1 not taken.
78231 callstack_push cs_push(callstack_entry_type::LINE, current_line, lineno);
920
921
6/6
✓ Branch 0 taken 1656 times.
✓ Branch 1 taken 161163 times.
✓ Branch 2 taken 828 times.
✓ Branch 3 taken 828 times.
✓ Branch 4 taken 774 times.
✓ Branch 5 taken 77457 times.
162819 if (stribegin(current_line, "macro ") && numif==numtrue)
922 {
923 // RPG Hacker: Slight redundancy here with code that is
924 // also in startmacro(). Could improve this for Asar 2.0.
925
1/2
✓ Branch 0 taken 774 times.
✗ Branch 1 not taken.
1548 string macro_name = current_line.data()+6;
926 1548 char * startpar=strqchr(macro_name.data(), '(');
927
1/2
✓ Branch 0 taken 1548 times.
✗ Branch 1 not taken.
1548 if (startpar) *startpar=0;
928
1/2
✓ Branch 0 taken 774 times.
✗ Branch 1 not taken.
1548 macro_defs.append(macro_name);
929
930 // RPG Hacker: I think it would make more logical sense
931 // to have this ++ after the if, but hat breaks compatibility
932 // with at least one test, and it generally leads to more
933 // errors being output after a broken macro declaration.
934 1548 in_macro_def++;
935
2/2
✓ Branch 0 taken 516 times.
✓ Branch 1 taken 1032 times.
1548 if (!pass)
936 {
937
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);
938
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
96 else tomacro(current_line);
939 }
940 1548 }
941
6/6
✓ Branch 0 taken 84624 times.
✓ Branch 1 taken 76647 times.
✓ Branch 2 taken 1566 times.
✓ Branch 3 taken 83058 times.
✓ Branch 4 taken 810 times.
✓ Branch 5 taken 77457 times.
161271 else if (!stricmp(current_line, "endmacro") && numif==numtrue)
942 {
943
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);
944 else
945 {
946 1512 in_macro_def--;
947
1/2
✓ Branch 0 taken 756 times.
✗ Branch 1 not taken.
1512 macro_defs.remove(in_macro_def);
948
2/2
✓ Branch 0 taken 504 times.
✓ Branch 1 taken 1008 times.
1512 if (!pass)
949 {
950
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);
951
1/2
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
90 else tomacro(current_line);
952 }
953 }
954 }
955
2/2
✓ Branch 0 taken 6840 times.
✓ Branch 1 taken 152919 times.
159759 else if (in_macro_def > 0)
956 {
957
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);
958 }
959 else
960 {
961
2/2
✓ Branch 0 taken 129417 times.
✓ Branch 1 taken 23502 times.
152919 assembleline(filename, lineno, current_line, single_line_for_tracker);
962 }
963 186729 }
964
2/2
✓ Branch 0 taken 23508 times.
✓ Branch 1 taken 402 times.
23910 catch (errline&) {}
965
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 123033 times.
123213 return (numif != prevnumif || single_line_for_tracker == 3)
966
9/10
✓ Branch 0 taken 123213 times.
✓ Branch 1 taken 16500 times.
✓ Branch 2 taken 12867 times.
✓ Branch 3 taken 3813 times.
✓ Branch 4 taken 7933 times.
✓ Branch 5 taken 4934 times.
✓ Branch 6 taken 4527 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1121 times.
✓ Branch 9 taken 3406 times.
279516 && (whilestatus[numif].iswhile || whilestatus[numif].is_for);
967 }
968
969
970 473 void parse_std_includes(const char* textfile, autoarray<string>& outarray)
971 {
972 473 char* content = readfilenative(textfile);
973
974
1/2
✓ Branch 0 taken 473 times.
✗ Branch 1 not taken.
473 if (content != nullptr)
975 {
976 237 char* pos = content;
977
978
2/2
✓ Branch 0 taken 948 times.
✓ Branch 1 taken 473 times.
1421 while (pos[0] != '\0')
979 {
980 476 string stdinclude;
981
982 do
983 {
984
3/4
✓ Branch 0 taken 23394 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22919 times.
✓ Branch 3 taken 475 times.
23394 if (pos[0] != '\r' && pos[0] != '\n')
985 {
986
1/2
✓ Branch 0 taken 13715 times.
✗ Branch 1 not taken.
22919 stdinclude += pos[0];
987 }
988 23394 pos++;
989
4/4
✓ Branch 0 taken 22921 times.
✓ Branch 1 taken 473 times.
✓ Branch 2 taken 22446 times.
✓ Branch 3 taken 475 times.
23394 } while (pos[0] != '\0' && pos[0] != '\n');
990
991
1/2
✓ Branch 0 taken 476 times.
✗ Branch 1 not taken.
948 strip_whitespace(stdinclude);
992
993
2/2
✓ Branch 0 taken 475 times.
✓ Branch 1 taken 473 times.
948 if (stdinclude != "")
994 {
995
3/4
✓ Branch 0 taken 475 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 474 times.
475 if (!path_is_absolute(stdinclude))
996 {
997
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 stdinclude = dir(textfile) + stdinclude;
998 }
999
2/4
✓ Branch 0 taken 475 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 239 times.
✗ Branch 3 not taken.
475 outarray.append(normalize_path(stdinclude));
1000 }
1001 948 }
1002
1003 473 free(content);
1004 }
1005 473 }
1006
1007 722 void parse_std_defines(const char* textfile)
1008 {
1009
1010 // RPG Hacker: add built-in defines.
1011 // (They're not really standard defines, but I was lazy and this was
1012 // one convenient place for doing it).
1013 722 builtindefines.create("assembler") = "asar";
1014
7/11
✓ Branch 0 taken 604 times.
✓ Branch 1 taken 118 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 253 times.
✓ Branch 4 taken 118 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 253 times.
✓ Branch 7 taken 118 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 253 times.
✗ Branch 10 not taken.
722 builtindefines.create("assembler_ver") = dec(get_version_int());
1015
3/6
✓ Branch 0 taken 722 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 371 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 371 times.
✗ Branch 5 not taken.
722 builtindefines.create("assembler_time") = dec(time(nullptr));
1016
1017
2/2
✓ Branch 0 taken 370 times.
✓ Branch 1 taken 352 times.
722 if(textfile == nullptr) return;
1018
1019 473 char* content = readfilenative(textfile);
1020
1021
1/2
✓ Branch 0 taken 473 times.
✗ Branch 1 not taken.
473 if (content != nullptr)
1022 {
1023 237 char* pos = content;
1024
2/2
✓ Branch 0 taken 2837 times.
✓ Branch 1 taken 473 times.
3310 while (*pos != 0) {
1025 1421 string define_name;
1026 1421 string define_val;
1027
1028
4/4
✓ Branch 0 taken 28347 times.
✓ Branch 1 taken 1890 times.
✓ Branch 2 taken 27400 times.
✓ Branch 3 taken 947 times.
30237 while (*pos != '=' && *pos != '\n') {
1029
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27400 times.
27400 if(*pos == '\r') { pos++; continue; }
1030
1/2
✓ Branch 0 taken 13712 times.
✗ Branch 1 not taken.
27400 define_name += *pos;
1031 27400 pos++;
1032 }
1033
5/6
✓ Branch 0 taken 2365 times.
✓ Branch 1 taken 472 times.
✓ Branch 2 taken 1421 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 946 times.
✓ Branch 5 taken 475 times.
2837 if (*pos != 0 && *pos != '\r' && *pos != '\n') pos++; // skip =
1034
3/4
✓ Branch 0 taken 12762 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9925 times.
✓ Branch 3 taken 2837 times.
12762 while (*pos != 0 && *pos != '\n') {
1035
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9925 times.
9925 if(*pos == '\r') { pos++; continue; }
1036
1/2
✓ Branch 0 taken 4969 times.
✗ Branch 1 not taken.
9925 define_val += *pos;
1037 9925 pos++;
1038 }
1039
1/2
✓ Branch 0 taken 2837 times.
✗ Branch 1 not taken.
2837 if (*pos != 0)
1040 2837 pos++; // skip \n
1041 // clean define_name
1042
1/2
✓ Branch 0 taken 1421 times.
✗ Branch 1 not taken.
2837 strip_whitespace(define_name);
1043
1/2
✓ Branch 0 taken 1421 times.
✗ Branch 1 not taken.
2837 define_name.strip_prefix('!'); // remove leading ! if present
1044
1045
2/2
✓ Branch 0 taken 473 times.
✓ Branch 1 taken 2364 times.
2837 if (define_name == "")
1046 {
1047
1/2
✓ Branch 0 taken 473 times.
✗ Branch 1 not taken.
473 if (define_val == "")
1048 {
1049 473 continue;
1050 }
1051
1052 asar_throw_error(pass, error_type_null, error_id_stddefines_no_identifier);
1053 }
1054
1055
3/8
✓ Branch 0 taken 594 times.
✓ Branch 1 taken 1770 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 594 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
2364 if (!validatedefinename(define_name)) asar_throw_error(pass, error_type_null, error_id_cmdl_define_invalid, "stddefines.txt", define_name.data());
1056
1057 // clean define_val
1058 1184 const char* defval = define_val.data();
1059 1184 string cleaned_defval;
1060
1061
2/2
✓ Branch 0 taken 474 times.
✓ Branch 1 taken 1890 times.
2364 if (*defval == 0) {
1062 // no value
1063
3/6
✓ Branch 0 taken 238 times.
✓ Branch 1 taken 236 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 238 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
474 if (clidefines.exists(define_name)) asar_throw_error(pass, error_type_null, error_id_cmdl_define_override, "Std define", define_name.data());
1064
2/4
✓ Branch 0 taken 474 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 238 times.
✗ Branch 3 not taken.
474 clidefines.create(define_name) = "";
1065 474 continue;
1066 }
1067
1068
3/4
✓ Branch 0 taken 946 times.
✓ Branch 1 taken 1890 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1890 times.
2836 while (*defval == ' ' || *defval == '\t') defval++; // skip whitespace in beginning
1069
2/2
✓ Branch 0 taken 473 times.
✓ Branch 1 taken 1417 times.
1890 if (*defval == '"') {
1070 473 defval++; // skip opening quote
1071
3/4
✓ Branch 0 taken 6141 times.
✓ Branch 1 taken 473 times.
✓ Branch 2 taken 6141 times.
✗ Branch 3 not taken.
6614 while (*defval != '"' && *defval != 0)
1072
1/2
✓ Branch 0 taken 3073 times.
✗ Branch 1 not taken.
6141 cleaned_defval += *defval++;
1073
1074
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 473 times.
473 if (*defval == 0) {
1075 asar_throw_error(pass, error_type_null, error_id_mismatched_quotes);
1076 }
1077 473 defval++; // skip closing quote
1078
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 473 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 473 times.
473 while (*defval == ' ' || *defval == '\t') defval++; // skip whitespace
1079
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 473 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
473 if (*defval != 0 && *defval != '\n')
1080 asar_throw_error(pass, error_type_null, error_id_stddefine_after_closing_quote);
1081
1082
3/6
✓ Branch 0 taken 237 times.
✓ Branch 1 taken 236 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 237 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
473 if (clidefines.exists(define_name)) asar_throw_error(pass, error_type_null, error_id_cmdl_define_override, "Std define", define_name.data());
1083
2/4
✓ Branch 0 taken 473 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 237 times.
✗ Branch 3 not taken.
473 clidefines.create(define_name) = cleaned_defval;
1084 473 continue;
1085 }
1086 else
1087 {
1088 // slightly hacky way to remove trailing whitespace
1089 1417 const char* defval_end = strchr(defval, '\n'); // slightly hacky way to get end of string or newline
1090
1/2
✓ Branch 0 taken 1417 times.
✗ Branch 1 not taken.
1417 if (!defval_end) defval_end = strchr(defval, 0);
1091 1417 defval_end--;
1092
3/4
✓ Branch 0 taken 475 times.
✓ Branch 1 taken 1417 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1417 times.
1892 while (*defval_end == ' ' || *defval_end == '\t') defval_end--;
1093
3/4
✓ Branch 0 taken 709 times.
✓ Branch 1 taken 708 times.
✓ Branch 2 taken 709 times.
✗ Branch 3 not taken.
1417 cleaned_defval = string(defval, (int)(defval_end - defval + 1));
1094
1095
3/6
✓ Branch 0 taken 709 times.
✓ Branch 1 taken 708 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 709 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1417 if (clidefines.exists(define_name)) asar_throw_error(pass, error_type_null, error_id_cmdl_define_override, "Std define", define_name.data());
1096
2/4
✓ Branch 0 taken 1417 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 709 times.
✗ Branch 3 not taken.
1417 clidefines.create(define_name) = cleaned_defval;
1097 1417 continue;
1098 1417 }
1099
1100 2837 }
1101 473 free(content);
1102 }
1103 }
1104
1105 bool checksum_fix_enabled = true;
1106 bool force_checksum_fix = false;
1107
1108 #define cfree(x) free((void*)x)
1109 414 static void clearmacro(const string & key, macrodata* & macro)
1110 {
1111 (void)key;
1112 414 freemacro(macro);
1113 414 }
1114
1115 838 static void clearfile(const string & key, sourcefile& filecontent)
1116 {
1117 (void)key;
1118 838 cfree(filecontent.data);
1119 838 cfree(filecontent.contents);
1120 838 }
1121 #undef cfree
1122
1123
1/2
✓ Branch 0 taken 2233 times.
✗ Branch 1 not taken.
4526 static void adddefine(const string & key, string & value)
1124 {
1125
1/2
✓ Branch 0 taken 4526 times.
✗ Branch 1 not taken.
4526 if (!defines.exists(key)) defines.create(key) = value;
1126 4526 }
1127
1128 static string symbolfile;
1129
1130 326 static void printsymbol_wla(const string& key, snes_label& label)
1131 {
1132
7/14
✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 163 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 163 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 163 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 163 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 163 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 163 times.
✗ Branch 13 not taken.
489 string line = hex((label.pos & 0xFF0000)>>16, 2)+":"+hex(label.pos & 0xFFFF, 4)+" "+key+"\n";
1133
1/2
✓ Branch 0 taken 163 times.
✗ Branch 1 not taken.
326 symbolfile += line;
1134 326 }
1135
1136 static void printsymbol_nocash(const string& key, snes_label& label)
1137 {
1138 string line = hex(label.pos & 0xFFFFFF, 8)+" "+key+"\n";
1139 symbolfile += line;
1140 }
1141
1142 164 string create_symbols_file(string format, uint32_t romCrc){
1143 164 format = lower(format);
1144 82 symbolfile = "";
1145
1/2
✓ Branch 0 taken 164 times.
✗ Branch 1 not taken.
164 if(format == "wla")
1146 {
1147 82 symbolfile = "; wla symbolic information file\n";
1148 164 symbolfile += "; generated by asar\n";
1149
1150 164 symbolfile += "\n[labels]\n";
1151 164 labels.each(printsymbol_wla);
1152
1153 164 symbolfile += "\n[source files]\n";
1154 82 const autoarray<AddressToLineMapping::FileInfo>& addrToLineFileList = addressToLineMapping.getFileList();
1155
2/2
✓ Branch 0 taken 174 times.
✓ Branch 1 taken 164 times.
338 for (int i = 0; i < addrToLineFileList.count; ++i)
1156 {
1157 char addrToFileListStr[256];
1158 261 snprintf(addrToFileListStr, 256, "%.4x %.8x %s\n",
1159 i,
1160
1/2
✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
174 addrToLineFileList[i].fileCrc,
1161
1/2
✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
87 addrToLineFileList[i].filename.data()
1162 );
1163
1/2
✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
174 symbolfile += addrToFileListStr;
1164 }
1165
1166 164 symbolfile += "\n[rom checksum]\n";
1167 {
1168 char romCrcStr[32];
1169 164 snprintf(romCrcStr, 32, "%.8x\n",
1170 romCrc
1171 );
1172
1/2
✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
164 symbolfile += romCrcStr;
1173 }
1174
1175 164 symbolfile += "\n[addr-to-line mapping]\n";
1176 82 const autoarray<AddressToLineMapping::AddrToLineInfo>& addrToLineInfo = addressToLineMapping.getAddrToLineInfo();
1177
2/2
✓ Branch 0 taken 5282 times.
✓ Branch 1 taken 164 times.
5446 for (int i = 0; i < addrToLineInfo.count; ++i)
1178 {
1179 char addrToLineStr[32];
1180 13205 snprintf(addrToLineStr, 32, "%.2x:%.4x %.4x:%.8x\n",
1181 7923 (addrToLineInfo[i].addr & 0xFF0000) >> 16,
1182
1/2
✓ Branch 0 taken 2641 times.
✗ Branch 1 not taken.
5282 addrToLineInfo[i].addr & 0xFFFF,
1183
1/2
✓ Branch 0 taken 2641 times.
✗ Branch 1 not taken.
5282 addrToLineInfo[i].fileIdx & 0xFFFF,
1184
2/4
✓ Branch 0 taken 2641 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2641 times.
✗ Branch 3 not taken.
5282 addrToLineInfo[i].line & 0xFFFFFFFF
1185 );
1186
1/2
✓ Branch 0 taken 2641 times.
✗ Branch 1 not taken.
5282 symbolfile += addrToLineStr;
1187 }
1188
1189 }
1190 else if (format == "nocash")
1191 {
1192 symbolfile = ";no$sns symbolic information file\n";
1193 symbolfile += ";generated by asar\n";
1194 symbolfile += "\n";
1195 labels.each(printsymbol_nocash);
1196 }
1197 164 return symbolfile;
1198 }
1199
1200
1201 6 bool in_top_level_file()
1202 {
1203 3 int num_files = 0;
1204
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 6 times.
30 for (int i = callstack.count-1; i >= 0; --i)
1205 {
1206
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 18 times.
24 if (callstack[i].type == callstack_entry_type::FILE)
1207 {
1208 6 num_files++;
1209
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (num_files > 1) break;
1210 }
1211 }
1212 6 return (num_files <= 1);
1213 }
1214
1215 43291 const char* get_current_file_name()
1216 {
1217
2/2
✓ Branch 0 taken 163592 times.
✓ Branch 1 taken 2107 times.
165699 for (int i = callstack.count-1; i >= 0; --i)
1218 {
1219
2/2
✓ Branch 0 taken 41184 times.
✓ Branch 1 taken 122408 times.
163592 if (callstack[i].type == callstack_entry_type::FILE)
1220 41184 return callstack[i].content.raw();
1221 }
1222 1084 return nullptr;
1223 }
1224
1225 26353 int get_current_line()
1226 {
1227
2/2
✓ Branch 0 taken 77948 times.
✓ Branch 1 taken 21 times.
77969 for (int i = callstack.count-1; i >= 0; --i)
1228 {
1229
2/2
✓ Branch 0 taken 26332 times.
✓ Branch 1 taken 51616 times.
91104 if (callstack[i].type == callstack_entry_type::LINE) return callstack[i].lineno;
1230 }
1231 11 return -1;
1232 }
1233
1234 700 const char* get_current_block()
1235 {
1236
2/2
✓ Branch 0 taken 705 times.
✓ Branch 1 taken 31 times.
736 for (int i = callstack.count-1; i >= 0; --i)
1237 {
1238
6/6
✓ Branch 0 taken 537 times.
✓ Branch 1 taken 168 times.
✓ Branch 2 taken 501 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 336 times.
✓ Branch 5 taken 18 times.
1305 if (callstack[i].type == callstack_entry_type::LINE || callstack[i].type == callstack_entry_type::BLOCK) return callstack[i].content.raw();
1239 }
1240 16 return nullptr;
1241 }
1242
1243
1244 727 void reseteverything()
1245 {
1246 374 string str;
1247 727 labels.reset();
1248 727 defines.reset();
1249
1/2
✓ Branch 0 taken 727 times.
✗ Branch 1 not taken.
727 builtindefines.each(adddefine);
1250
1/2
✓ Branch 0 taken 727 times.
✗ Branch 1 not taken.
727 clidefines.each(adddefine);
1251 727 structs.reset();
1252
1253
1/2
✓ Branch 0 taken 727 times.
✗ Branch 1 not taken.
727 macros.each(clearmacro);
1254 727 macros.reset();
1255
1256
1/2
✓ Branch 0 taken 727 times.
✗ Branch 1 not taken.
727 filecontents.each(clearfile);
1257 727 filecontents.reset();
1258
1259 727 writtenblocks.reset();
1260
1261 727 optimizeforbank=-1;
1262 727 optimize_dp = optimize_dp_flag::NONE;
1263 727 dp_base = 0;
1264 727 optimize_address = optimize_address_flag::DEFAULT;
1265
1266
1/2
✓ Branch 0 taken 727 times.
✗ Branch 1 not taken.
727 closecachedfiles();
1267
1268 727 incsrcdepth=0;
1269 727 label_counter = 0;
1270 727 errored = false;
1271 727 checksum_fix_enabled = true;
1272 727 force_checksum_fix = false;
1273
1274 727 in_macro_def = 0;
1275
1276 #ifndef ASAR_SHARED
1277 236 free(const_cast<unsigned char*>(romdata_r));
1278 #endif
1279
1280
1/2
✓ Branch 0 taken 374 times.
✗ Branch 1 not taken.
727 callstack.reset();
1281 727 simple_callstacks = true;
1282 #undef free
1283 727 }
1284