-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtest.py
executable file
·520 lines (438 loc) · 15.9 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#!/usr/bin/env python3
from spimdisasm.common import FileSectionType
from src.splat.scripts.split import *
import unittest
import io
import filecmp
from src.splat.util import symbols, options
import spimdisasm
from src.splat.segtypes.common.rodata import CommonSegRodata
from src.splat.segtypes.common.code import CommonSegCode
from src.splat.segtypes.common.c import CommonSegC
from src.splat.segtypes.common.bss import CommonSegBss
from src.splat import __version__
import difflib
class Testing(unittest.TestCase):
def compare_files(self, test_path, ref_path):
with io.open(test_path) as test_f, io.open(ref_path) as ref_f:
self.assertListEqual(list(test_f), list(ref_f))
def get_same_files(self, dcmp, out):
for name in dcmp.same_files:
out.append((name, dcmp.left, dcmp.right))
for sub_dcmp in dcmp.subdirs.values():
self.get_same_files(sub_dcmp, out)
def get_diff_files(self, dcmp, out):
for name in dcmp.diff_files:
out.append((name, dcmp.left, dcmp.right))
for sub_dcmp in dcmp.subdirs.values():
self.get_diff_files(sub_dcmp, out)
def get_left_only_files(self, dcmp, out):
for name in dcmp.left_only:
out.append((name, dcmp.left, dcmp.right))
for sub_dcmp in dcmp.subdirs.values():
self.get_left_only_files(sub_dcmp, out)
def get_right_only_files(self, dcmp, out):
for name in dcmp.right_only:
out.append((name, dcmp.left, dcmp.right))
for sub_dcmp in dcmp.subdirs.values():
self.get_right_only_files(sub_dcmp, out)
def test_basic_app(self):
spimdisasm.common.GlobalConfig.ASM_GENERATED_BY = False
main(["test/basic_app/splat.yaml"], None, False)
comparison = filecmp.dircmp(
"test/basic_app/split", "test/basic_app/expected", [".gitkeep"]
)
diff_files: List[Tuple[str, str, str]] = []
self.get_diff_files(comparison, diff_files)
same_files: List[Tuple[str, str, str]] = []
self.get_same_files(comparison, same_files)
left_only_files: List[Tuple[str, str, str]] = []
self.get_left_only_files(comparison, left_only_files)
right_only_files: List[Tuple[str, str, str]] = []
self.get_right_only_files(comparison, right_only_files)
print("same_files", same_files)
print("diff_files", diff_files)
print("left_only_files", left_only_files)
print("right_only_files", right_only_files)
# if the files are different print out the difference
for file in diff_files:
# can't diff binary
if file[0] == ".splache":
continue
with open(f"{file[1]}/{file[0]}") as file1:
file1_lines = file1.readlines()
with open(f"{file[2]}/{file[0]}") as file2:
file2_lines = file2.readlines()
for line in difflib.unified_diff(
file1_lines, file2_lines, fromfile="file1", tofile="file2", lineterm=""
):
print(line)
assert len(diff_files) == 0, diff_files
assert len(left_only_files) == 0, left_only_files
assert len(right_only_files) == 0, right_only_files
def test_init():
options_dict = {
"options": {
"platform": "n64",
"compiler": "GCC",
"basename": "basic_app",
"base_path": ".",
"build_path": "build",
"target_path": "build/main.bin",
"asm_path": "split/asm",
"src_path": "split/src",
"ld_script_path": "split/basic_app.ld",
"cache_path": "split/.splache",
"symbol_addrs_path": "split/generated.symbols.txt",
"undefined_funcs_auto_path": "split/undefined_funcs_auto.txt",
"undefined_syms_auto_path": "split/undefined_syms_auto.txt",
},
"segments": [
{
"name": "basic_app",
"type": "code",
"start": 0,
"vram": 0x400000,
"subalign": 4,
"subsegments": [[0, "data"], [0x1DC, "c", "main"], [0x1FC, "data"]],
},
[0x1290],
],
}
options.initialize(options_dict, ["./test/basic_app/splat.yaml"], [], False)
class Symbols(unittest.TestCase):
def test_check_valid_type(self):
options.opts.platform = "n64"
disassembler_instance.create_disassembler_instance(False, __version__)
# first char is uppercase
assert symbols.check_valid_type("Symbol")
splat_sym_types = {"func", "jtbl", "jtbl_label", "label"}
for type in splat_sym_types:
assert symbols.check_valid_type(type)
spim_types = [
"char*",
"u32",
"Vec3f",
"u8",
"char",
"u16",
"f32",
"u64",
"asciz",
"s8",
"s64",
"f64",
"s16",
"s32",
]
for type in spim_types:
assert symbols.check_valid_type(type)
def test_add_symbol_to_spim_segment(self):
segment = spimdisasm.common.SymbolsSegment(
context=spimdisasm.common.Context(),
vromStart=0x0,
vromEnd=0x10,
vramStart=0x40000000 + 0x0,
vramEnd=0x40000000 + 0x10,
)
sym = symbols.Symbol(0x40000000)
sym.user_declared = False
sym.defined = True
sym.rom = 0x0
sym.type = "func"
result = symbols.add_symbol_to_spim_segment(segment, sym)
assert result.type == spimdisasm.common.SymbolSpecialType.function
assert sym.user_declared == result.isUserDeclared
assert sym.defined == result.isDefined
def test_add_symbol_to_spim_section(self):
section = spimdisasm.mips.sections.SectionBase(
context=spimdisasm.common.Context(),
vromStart=0x0,
vromEnd=0x10,
vram=0x40000000,
filename="test",
words=[],
sectionType=FileSectionType.Text,
segmentVromStart=0x0,
overlayCategory=None,
)
sym = symbols.Symbol(0x100)
sym.type = "func"
sym.user_declared = False
sym.defined = True
result = symbols.add_symbol_to_spim_section(section, sym)
assert result.type == spimdisasm.common.SymbolSpecialType.function
assert sym.user_declared == result.isUserDeclared
assert sym.defined == result.isDefined
def test_create_symbol_from_spim_symbol(self):
# need to init otherwise options.opts isn't defined.
# used in initializing a Segment
test_init()
segment = Segment(
rom_start=0x0,
rom_end=0x100,
type="func",
name="MyFunc",
vram_start=0x40000000,
args=[],
yaml=None,
)
context_sym = spimdisasm.common.ContextSymbol(address=0)
result = symbols.create_symbol_from_spim_symbol(segment, context_sym)
assert result.referenced
assert result.extract
assert result.name == "D_0"
def get_yaml():
return {
"name": "basic_app",
"type": "code",
"start": 0,
"vram": 0x400000,
"subalign": 4,
"subsegments": [[0, "data"], [0x1DC, "c", "main"], [0x1FC, "data"]],
}
class Rodata(unittest.TestCase):
def test_disassemble_data(self):
test_init()
common_seg_rodata = CommonSegRodata(
rom_start=0x0,
rom_end=0x100,
type=".rodata",
name="MyRodata",
vram_start=0x400,
args=None,
yaml=None,
)
rom_data = []
for i in range(0x100):
rom_data.append(i)
common_seg_rodata.disassemble_data(bytes(rom_data))
assert common_seg_rodata.spim_section is not None
assert common_seg_rodata.spim_section.get_section().words[0] == 0x0010203
assert symbols.get_all_symbols()[0].vram_start == 0x400
assert symbols.get_all_symbols()[0].segment == common_seg_rodata
assert symbols.get_all_symbols()[0].linker_section == ".rodata"
def test_get_possible_text_subsegment_for_symbol(self):
context = spimdisasm.common.Context()
result_symbol_addr = 0x2DC
# use SymbolRodata to test migration
rodata_sym = spimdisasm.mips.symbols.SymbolRodata(
context=context,
vromStart=0x100,
vromEnd=0x200,
inFileOffset=0,
vram=0x100,
words=[0, 1, 2, 3, 4, 5, 6, 7],
segmentVromStart=0,
overlayCategory=None,
)
rodata_sym.contextSym.forceMigration = True
context_sym = spimdisasm.common.ContextSymbol(address=0)
context_sym.address = result_symbol_addr
rodata_sym.contextSym.referenceFunctions = {context_sym}
# Segment __init__ requires opts to be initialized
test_init()
common_seg_rodata = CommonSegRodata(
rom_start=0x0,
rom_end=0x100,
type=".rodata",
name="MyRodata",
vram_start=0x400,
args=None,
yaml=None,
)
common_seg_rodata.parent = CommonSegCode(
rom_start=0x0,
rom_end=0x200,
type="code",
name="MyCode",
vram_start=0x100,
args=[],
yaml=get_yaml(),
)
result = common_seg_rodata.get_possible_text_subsegment_for_symbol(rodata_sym)
assert result is not None
assert type(result[0]) == CommonSegC
assert result[1].address == result_symbol_addr
class Bss(unittest.TestCase):
def test_disassemble_data(self):
# Segment __init__ requires opts to be initialized
test_init()
bss = CommonSegBss(
rom_start=0x0,
rom_end=0x100,
type=".bss",
name=None,
vram_start=0x40000000,
args=None,
yaml=None,
)
bss.parent = CommonSegCode(
rom_start=0x0,
rom_end=0x200,
type="code",
name="MyCode",
vram_start=0x100,
args=[],
yaml=get_yaml(),
)
rom_bytes = bytes([0, 1, 2, 3, 4, 5, 6, 7])
bss.disassemble_data(rom_bytes)
assert bss.spim_section is not None
assert isinstance(
bss.spim_section.get_section(), spimdisasm.mips.sections.SectionBss
)
assert bss.spim_section.get_section().bssVramStart == 0x40000000
assert bss.spim_section.get_section().bssVramEnd == 0x300
class SymbolsInitialize(unittest.TestCase):
def test_attrs(self):
import pathlib
symbols.reset_symbols()
test_init()
sym_addrs_lines = [
"func_1 = 0x100; // type:func size:10 rom:100 segment:test_segment name_end:the_name_end "
]
all_segments = [
Segment(
rom_start=0x100,
rom_end=0x200,
type="func",
name="test_segment",
vram_start=0x300,
args=[],
yaml={},
)
]
symbols.handle_sym_addrs(
pathlib.Path("/tmp/thing"), sym_addrs_lines, all_segments
)
assert symbols.all_symbols[0].given_name == "func_1"
assert symbols.all_symbols[0].type == "func"
assert symbols.all_symbols[0].given_size == 10
assert symbols.all_symbols[0].rom == 100
assert symbols.all_symbols[0].segment == all_segments[0]
assert symbols.all_symbols[0].given_name_end == "the_name_end"
def test_boolean_attrs(self):
import pathlib
symbols.reset_symbols()
test_init()
sym_addrs_lines = [
"func_1 = 0x100; // defined:True extract:True force_migration:True force_not_migration:True "
"allow_addend:True dont_allow_addend:True"
]
all_segments = [
Segment(
rom_start=0x100,
rom_end=0x200,
type="func",
name="test_segment",
vram_start=0x300,
args=[],
yaml={},
)
]
symbols.handle_sym_addrs(
pathlib.Path("/tmp/thing"), sym_addrs_lines, all_segments
)
assert symbols.all_symbols[0].defined == True
assert symbols.all_symbols[0].force_migration == True
assert symbols.all_symbols[0].force_not_migration == True
assert symbols.all_symbols[0].allow_addend == True
assert symbols.all_symbols[0].dont_allow_addend == True
# test spim ban range
def test_ignore(self):
import pathlib
symbols.reset_symbols()
test_init()
sym_addrs_lines = ["func_1 = 0x100; // ignore:True size:4"]
all_segments = [
Segment(
rom_start=0x100,
rom_end=0x200,
type="func",
name="test_segment",
vram_start=0x300,
args=[],
yaml={},
)
]
symbols.handle_sym_addrs(
pathlib.Path("/tmp/thing"), sym_addrs_lines, all_segments
)
assert symbols.spim_context.bannedRangedSymbols[0].start == 0x100
assert symbols.spim_context.bannedRangedSymbols[0].end == 0x100 + 4
class InitializeSpimContext(unittest.TestCase):
def test_overlay(self):
symbols.reset_symbols()
test_init()
yaml = {
"name": "boot",
"type": "code",
"start": 0x1000,
"vram": 0x80000400,
"bss_size": 0x80,
"exclusive_ram_id": "overlay",
"subsegments": [
[0x1000, "c", "main"],
[0x10F0, "hasm", "handwritten"],
[0x1100, "data", "main"],
[0x1110, ".rodata", "main"],
{"start": 0x1140, "type": "bss", "vram": 0x80000540, "name": "main"},
],
}
all_segments: List["Segment"] = [
CommonSegCode(
rom_start=0x0,
rom_end=0x200,
type="code",
name="main",
vram_start=0x100,
args=[],
yaml=yaml,
)
]
# force this since it's hard to set up
all_segments[0].exclusive_ram_id = "overlay"
symbols.initialize_spim_context(all_segments)
# spim should have added something to overlaySegments
assert (
type(symbols.spim_context.overlaySegments["overlay"][0])
== spimdisasm.common.SymbolsSegment
)
# test globalSegment settings
def test_global(self):
symbols.reset_symbols()
test_init()
yaml = {
"name": "boot",
"type": "code",
"start": 0x1000,
"vram": 0x80000400,
"bss_size": 0x80,
"exclusive_ram_id": "overlay",
"subsegments": [
[0x1000, "c", "main"],
[0x10F0, "hasm", "handwritten"],
[0x1100, "data", "main"],
[0x1110, ".rodata", "main"],
{"start": 0x1140, "type": "bss", "vram": 0x80000540, "name": "main"},
],
}
all_segments: List["Segment"] = [
CommonSegCode(
rom_start=0x0,
rom_end=0x200,
type="code",
name="main",
vram_start=0x100,
args=[],
yaml=yaml,
)
]
assert symbols.spim_context.globalSegment.vramStart == 0x80000000
assert symbols.spim_context.globalSegment.vramEnd == 0x80001000
symbols.initialize_spim_context(all_segments)
assert symbols.spim_context.globalSegment.vramStart == 0x100
assert symbols.spim_context.globalSegment.vramEnd == 0x380
if __name__ == "__main__":
unittest.main()