Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export lower parts of external entries to object file #61

Merged
merged 2 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cocas/assembler/targets/cdm8e/code_segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ def add_ext_record(obj_rec: "ObjectSectionRecord", ext: str, s: "Section", val:
if seg.expr.byte_specifier == 'low':
obj_rec.external.setdefault(ext, []).append(ExternalEntry(offset, range(0, 1), full_bytes=False))
elif seg.expr.byte_specifier == 'high':
obj_rec.external.setdefault(ext, []).append(ExternalEntry(offset, range(1, 2), full_bytes=False))
obj_rec.lower_parts[offset] = obj_rec.lower_parts.get(offset, 0) + val_lo
entry = ExternalEntry(offset, range(1, 2), full_bytes=False, lower_part=val_lo)
obj_rec.external.setdefault(ext, []).append(entry)
else:
obj_rec.external.setdefault(ext, []).append(ExternalEntry(offset, range(0, 2), full_bytes=True))

Expand All @@ -276,7 +276,7 @@ def add_rel_record(obj_rec: "ObjectSectionRecord", s: "Section", val: int,
if seg.expr.byte_specifier == 'low':
obj_rec.relocatable.append(ExternalEntry(offset, range(0, 1), full_bytes=False))
elif seg.expr.byte_specifier == 'high':
obj_rec.relocatable.append(ExternalEntry(offset, range(1, 2), full_bytes=False))
obj_rec.lower_parts[offset] = obj_rec.lower_parts.get(offset, 0) + val_lo
entry = ExternalEntry(offset, range(1, 2), full_bytes=False, lower_part=val_lo)
obj_rec.relocatable.append(entry)
else:
obj_rec.relocatable.append(ExternalEntry(offset, range(0, 2), full_bytes=True))
45 changes: 23 additions & 22 deletions cocas/linker/linker.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,37 +130,38 @@ def link(objects: list[tuple[Any, ObjectModule]], image_size: Optional[int] = No
for loc_offset, location in asect.code_locations.items():
code_locations[loc_offset + image_begin] = location

lower_parts: dict[int, int] = {} # Won't be empty if two entries added together, currently targets don't do that
for rsect in rsects:
image_begin = sect_addresses[rsect.name]
image_end = image_begin + len(rsect.data)
image[image_begin:image_end] = rsect.data
entry_bytes: range
for offset, entry_bytes, sign in map(lambda x: x.as_tuple(), rsect.relocatable):
pos = image_begin + offset
lower_limit = 1 << 8 * entry_bytes.start
val = int.from_bytes(image[pos:pos + len(entry_bytes)], 'little', signed=False) * lower_limit
val += rsect.lower_parts.get(offset, 0)
val += image_begin * sign
val %= (1 << 8 * entry_bytes.stop)
if entry_bytes.start > 0:
rsect.lower_parts[pos] = val % lower_limit
image[pos:pos + len(entry_bytes)] = (val // lower_limit).to_bytes(len(entry_bytes), 'little', signed=False)
for entry in rsect.relocatable:
pos = image_begin + entry.offset
lower_limit = 1 << 8 * entry.entry_bytes.start
val = int.from_bytes(image[pos:pos + len(entry.entry_bytes)], 'little', signed=False) * lower_limit
val += entry.lower_part + lower_parts.get(entry.offset, 0)
val += image_begin * entry.sign
val %= (1 << 8 * entry.entry_bytes.stop)
if entry.entry_bytes.start > 0 and val % lower_limit != 0:
lower_parts[pos] = val % lower_limit
image[pos:pos + len(entry.entry_bytes)] = \
(val // lower_limit).to_bytes(len(entry.entry_bytes), 'little', signed=False)
for loc_offset, location in rsect.code_locations.items():
code_locations[loc_offset + image_begin] = location

for sect in asects + rsects:
for ext_name in sect.external:
for offset, entry_bytes, sign in map(lambda x: x.as_tuple(), sect.external[ext_name]):
pos = sect_addresses[sect.name] + offset
lower_limit = 1 << 8 * entry_bytes.start
val = int.from_bytes(image[pos:pos + len(entry_bytes)], 'little', signed=False) * lower_limit
val += sect.lower_parts.get(offset, 0)
val += ents[ext_name] * sign
val %= (1 << 8 * entry_bytes.stop)
image[pos:pos + len(entry_bytes)] = (val // lower_limit).to_bytes(len(entry_bytes), 'little',
signed=False)
if entry_bytes.start > 0:
sect.lower_parts[pos] = val % lower_limit
for entry in sect.external[ext_name]:
pos = sect_addresses[sect.name] + entry.offset
lower_limit = 1 << 8 * entry.entry_bytes.start
val = int.from_bytes(image[pos:pos + len(entry.entry_bytes)], 'little', signed=False) * lower_limit
val += entry.lower_part + lower_parts.get(entry.offset, 0)
val += ents[ext_name] * entry.sign
val %= (1 << 8 * entry.entry_bytes.stop)
image[pos:pos + len(entry.entry_bytes)] = \
(val // lower_limit).to_bytes(len(entry.entry_bytes), 'little', signed=False)
if entry.entry_bytes.start > 0 and val % lower_limit != 0:
lower_parts[pos] = val % lower_limit

return image, code_locations

Expand Down
156 changes: 79 additions & 77 deletions cocas/object_file/generated/ObjectFileLexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,70 +10,71 @@

def serializedATN():
return [
4,0,23,180,6,-1,6,-1,6,-1,6,-1,6,-1,2,0,7,0,2,1,7,1,2,2,7,2,2,3,
4,0,24,184,6,-1,6,-1,6,-1,6,-1,6,-1,2,0,7,0,2,1,7,1,2,2,7,2,2,3,
7,3,2,4,7,4,2,5,7,5,2,6,7,6,2,7,7,7,2,8,7,8,2,9,7,9,2,10,7,10,2,
11,7,11,2,12,7,12,2,13,7,13,2,14,7,14,2,15,7,15,2,16,7,16,2,17,7,
17,2,18,7,18,2,19,7,19,2,20,7,20,2,21,7,21,2,22,7,22,1,0,1,0,1,0,
1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,2,1,2,1,2,1,2,1,2,1,3,
1,3,1,3,1,3,1,4,1,4,1,4,1,4,1,4,1,5,1,5,1,5,1,5,1,5,1,6,1,6,1,6,
1,6,1,6,1,7,1,7,1,7,1,7,1,7,1,7,1,7,1,8,1,8,1,8,1,8,1,9,1,9,1,9,
1,9,1,9,1,10,4,10,106,8,10,11,10,12,10,107,1,11,1,11,1,11,1,11,1,
11,1,12,1,12,1,13,1,13,1,14,3,14,120,8,14,1,14,4,14,123,8,14,11,
14,12,14,124,1,15,4,15,128,8,15,11,15,12,15,129,1,15,1,15,1,16,3,
16,135,8,16,1,16,4,16,138,8,16,11,16,12,16,139,1,16,1,16,1,17,4,
17,145,8,17,11,17,12,17,146,1,18,4,18,150,8,18,11,18,12,18,151,1,
19,4,19,155,8,19,11,19,12,19,156,1,19,1,19,1,20,1,20,1,20,1,20,1,
20,1,21,4,21,167,8,21,11,21,12,21,168,1,21,1,21,1,21,1,22,4,22,175,
8,22,11,22,12,22,176,1,22,1,22,0,0,23,5,1,7,2,9,3,11,4,13,5,15,6,
17,7,19,8,21,9,23,10,25,11,27,12,29,13,31,14,33,15,35,16,37,17,39,
18,41,19,43,20,45,21,47,22,49,23,5,0,1,2,3,4,4,4,0,48,57,65,90,95,
95,97,122,2,0,9,9,32,32,6,0,9,9,32,32,48,57,65,90,95,95,97,122,2,
0,10,10,13,13,186,0,5,1,0,0,0,0,7,1,0,0,0,0,9,1,0,0,0,0,11,1,0,0,
0,0,13,1,0,0,0,0,15,1,0,0,0,0,17,1,0,0,0,0,19,1,0,0,0,0,21,1,0,0,
0,0,23,1,0,0,0,0,25,1,0,0,0,0,27,1,0,0,0,0,29,1,0,0,0,0,31,1,0,0,
0,0,33,1,0,0,0,0,35,1,0,0,0,1,37,1,0,0,0,1,39,1,0,0,0,2,41,1,0,0,
0,2,43,1,0,0,0,2,45,1,0,0,0,3,47,1,0,0,0,4,49,1,0,0,0,5,51,1,0,0,
0,7,56,1,0,0,0,9,63,1,0,0,0,11,69,1,0,0,0,13,73,1,0,0,0,15,78,1,
0,0,0,17,83,1,0,0,0,19,88,1,0,0,0,21,95,1,0,0,0,23,99,1,0,0,0,25,
105,1,0,0,0,27,109,1,0,0,0,29,114,1,0,0,0,31,116,1,0,0,0,33,122,
1,0,0,0,35,127,1,0,0,0,37,137,1,0,0,0,39,144,1,0,0,0,41,149,1,0,
0,0,43,154,1,0,0,0,45,160,1,0,0,0,47,166,1,0,0,0,49,174,1,0,0,0,
51,52,5,84,0,0,52,53,5,65,0,0,53,54,5,82,0,0,54,55,5,71,0,0,55,6,
1,0,0,0,56,57,5,70,0,0,57,58,5,73,0,0,58,59,5,76,0,0,59,60,5,69,
0,0,60,61,1,0,0,0,61,62,6,1,0,0,62,8,1,0,0,0,63,64,5,65,0,0,64,65,
5,66,0,0,65,66,5,83,0,0,66,67,1,0,0,0,67,68,6,2,1,0,68,10,1,0,0,
0,69,70,5,76,0,0,70,71,5,79,0,0,71,72,5,67,0,0,72,12,1,0,0,0,73,
74,5,78,0,0,74,75,5,84,0,0,75,76,5,82,0,0,76,77,5,89,0,0,77,14,1,
0,0,0,78,79,5,78,0,0,79,80,5,65,0,0,80,81,5,77,0,0,81,82,5,69,0,
0,82,16,1,0,0,0,83,84,5,65,0,0,84,85,5,76,0,0,85,86,5,73,0,0,86,
87,5,71,0,0,87,18,1,0,0,0,88,89,5,68,0,0,89,90,5,65,0,0,90,91,5,
84,0,0,91,92,5,65,0,0,92,93,1,0,0,0,93,94,6,7,2,0,94,20,1,0,0,0,
95,96,5,82,0,0,96,97,5,69,0,0,97,98,5,76,0,0,98,22,1,0,0,0,99,100,
5,88,0,0,100,101,5,84,0,0,101,102,5,82,0,0,102,103,5,78,0,0,103,
24,1,0,0,0,104,106,7,0,0,0,105,104,1,0,0,0,106,107,1,0,0,0,107,105,
1,0,0,0,107,108,1,0,0,0,108,26,1,0,0,0,109,110,5,36,0,0,110,111,
5,97,0,0,111,112,5,98,0,0,112,113,5,115,0,0,113,28,1,0,0,0,114,115,
5,58,0,0,115,30,1,0,0,0,116,117,5,45,0,0,117,32,1,0,0,0,118,120,
5,13,0,0,119,118,1,0,0,0,119,120,1,0,0,0,120,121,1,0,0,0,121,123,
5,10,0,0,122,119,1,0,0,0,123,124,1,0,0,0,124,122,1,0,0,0,124,125,
1,0,0,0,125,34,1,0,0,0,126,128,7,1,0,0,127,126,1,0,0,0,128,129,1,
0,0,0,129,127,1,0,0,0,129,130,1,0,0,0,130,131,1,0,0,0,131,132,6,
15,3,0,132,36,1,0,0,0,133,135,5,13,0,0,134,133,1,0,0,0,134,135,1,
0,0,0,135,136,1,0,0,0,136,138,5,10,0,0,137,134,1,0,0,0,138,139,1,
0,0,0,139,137,1,0,0,0,139,140,1,0,0,0,140,141,1,0,0,0,141,142,6,
16,4,0,142,38,1,0,0,0,143,145,7,2,0,0,144,143,1,0,0,0,145,146,1,
0,0,0,146,144,1,0,0,0,146,147,1,0,0,0,147,40,1,0,0,0,148,150,7,0,
0,0,149,148,1,0,0,0,150,151,1,0,0,0,151,149,1,0,0,0,151,152,1,0,
0,0,152,42,1,0,0,0,153,155,7,1,0,0,154,153,1,0,0,0,155,156,1,0,0,
0,156,154,1,0,0,0,156,157,1,0,0,0,157,158,1,0,0,0,158,159,6,19,3,
0,159,44,1,0,0,0,160,161,5,58,0,0,161,162,1,0,0,0,162,163,6,20,4,
0,163,164,6,20,2,0,164,46,1,0,0,0,165,167,5,32,0,0,166,165,1,0,0,
0,167,168,1,0,0,0,168,166,1,0,0,0,168,169,1,0,0,0,169,170,1,0,0,
0,170,171,6,21,4,0,171,172,6,21,5,0,172,48,1,0,0,0,173,175,8,3,0,
0,174,173,1,0,0,0,175,176,1,0,0,0,176,174,1,0,0,0,176,177,1,0,0,
0,177,178,1,0,0,0,178,179,6,22,4,0,179,50,1,0,0,0,16,0,1,2,3,4,107,
119,124,129,134,139,146,151,156,168,176,6,5,3,0,5,2,0,5,1,0,6,0,
0,4,0,0,5,4,0
17,2,18,7,18,2,19,7,19,2,20,7,20,2,21,7,21,2,22,7,22,2,23,7,23,1,
0,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,2,1,2,1,2,1,
2,1,2,1,3,1,3,1,3,1,3,1,4,1,4,1,4,1,4,1,4,1,5,1,5,1,5,1,5,1,5,1,
6,1,6,1,6,1,6,1,6,1,7,1,7,1,7,1,7,1,7,1,7,1,7,1,8,1,8,1,8,1,8,1,
9,1,9,1,9,1,9,1,9,1,10,4,10,108,8,10,11,10,12,10,109,1,11,1,11,1,
11,1,11,1,11,1,12,1,12,1,13,1,13,1,14,1,14,1,15,3,15,124,8,15,1,
15,4,15,127,8,15,11,15,12,15,128,1,16,4,16,132,8,16,11,16,12,16,
133,1,16,1,16,1,17,3,17,139,8,17,1,17,4,17,142,8,17,11,17,12,17,
143,1,17,1,17,1,18,4,18,149,8,18,11,18,12,18,150,1,19,4,19,154,8,
19,11,19,12,19,155,1,20,4,20,159,8,20,11,20,12,20,160,1,20,1,20,
1,21,1,21,1,21,1,21,1,21,1,22,4,22,171,8,22,11,22,12,22,172,1,22,
1,22,1,22,1,23,4,23,179,8,23,11,23,12,23,180,1,23,1,23,0,0,24,5,
1,7,2,9,3,11,4,13,5,15,6,17,7,19,8,21,9,23,10,25,11,27,12,29,13,
31,14,33,15,35,16,37,17,39,18,41,19,43,20,45,21,47,22,49,23,51,24,
5,0,1,2,3,4,4,4,0,48,57,65,90,95,95,97,122,2,0,9,9,32,32,6,0,9,9,
32,32,48,57,65,90,95,95,97,122,2,0,10,10,13,13,190,0,5,1,0,0,0,0,
7,1,0,0,0,0,9,1,0,0,0,0,11,1,0,0,0,0,13,1,0,0,0,0,15,1,0,0,0,0,17,
1,0,0,0,0,19,1,0,0,0,0,21,1,0,0,0,0,23,1,0,0,0,0,25,1,0,0,0,0,27,
1,0,0,0,0,29,1,0,0,0,0,31,1,0,0,0,0,33,1,0,0,0,0,35,1,0,0,0,0,37,
1,0,0,0,1,39,1,0,0,0,1,41,1,0,0,0,2,43,1,0,0,0,2,45,1,0,0,0,2,47,
1,0,0,0,3,49,1,0,0,0,4,51,1,0,0,0,5,53,1,0,0,0,7,58,1,0,0,0,9,65,
1,0,0,0,11,71,1,0,0,0,13,75,1,0,0,0,15,80,1,0,0,0,17,85,1,0,0,0,
19,90,1,0,0,0,21,97,1,0,0,0,23,101,1,0,0,0,25,107,1,0,0,0,27,111,
1,0,0,0,29,116,1,0,0,0,31,118,1,0,0,0,33,120,1,0,0,0,35,126,1,0,
0,0,37,131,1,0,0,0,39,141,1,0,0,0,41,148,1,0,0,0,43,153,1,0,0,0,
45,158,1,0,0,0,47,164,1,0,0,0,49,170,1,0,0,0,51,178,1,0,0,0,53,54,
5,84,0,0,54,55,5,65,0,0,55,56,5,82,0,0,56,57,5,71,0,0,57,6,1,0,0,
0,58,59,5,70,0,0,59,60,5,73,0,0,60,61,5,76,0,0,61,62,5,69,0,0,62,
63,1,0,0,0,63,64,6,1,0,0,64,8,1,0,0,0,65,66,5,65,0,0,66,67,5,66,
0,0,67,68,5,83,0,0,68,69,1,0,0,0,69,70,6,2,1,0,70,10,1,0,0,0,71,
72,5,76,0,0,72,73,5,79,0,0,73,74,5,67,0,0,74,12,1,0,0,0,75,76,5,
78,0,0,76,77,5,84,0,0,77,78,5,82,0,0,78,79,5,89,0,0,79,14,1,0,0,
0,80,81,5,78,0,0,81,82,5,65,0,0,82,83,5,77,0,0,83,84,5,69,0,0,84,
16,1,0,0,0,85,86,5,65,0,0,86,87,5,76,0,0,87,88,5,73,0,0,88,89,5,
71,0,0,89,18,1,0,0,0,90,91,5,68,0,0,91,92,5,65,0,0,92,93,5,84,0,
0,93,94,5,65,0,0,94,95,1,0,0,0,95,96,6,7,2,0,96,20,1,0,0,0,97,98,
5,82,0,0,98,99,5,69,0,0,99,100,5,76,0,0,100,22,1,0,0,0,101,102,5,
88,0,0,102,103,5,84,0,0,103,104,5,82,0,0,104,105,5,78,0,0,105,24,
1,0,0,0,106,108,7,0,0,0,107,106,1,0,0,0,108,109,1,0,0,0,109,107,
1,0,0,0,109,110,1,0,0,0,110,26,1,0,0,0,111,112,5,36,0,0,112,113,
5,97,0,0,113,114,5,98,0,0,114,115,5,115,0,0,115,28,1,0,0,0,116,117,
5,58,0,0,117,30,1,0,0,0,118,119,5,45,0,0,119,32,1,0,0,0,120,121,
5,43,0,0,121,34,1,0,0,0,122,124,5,13,0,0,123,122,1,0,0,0,123,124,
1,0,0,0,124,125,1,0,0,0,125,127,5,10,0,0,126,123,1,0,0,0,127,128,
1,0,0,0,128,126,1,0,0,0,128,129,1,0,0,0,129,36,1,0,0,0,130,132,7,
1,0,0,131,130,1,0,0,0,132,133,1,0,0,0,133,131,1,0,0,0,133,134,1,
0,0,0,134,135,1,0,0,0,135,136,6,16,3,0,136,38,1,0,0,0,137,139,5,
13,0,0,138,137,1,0,0,0,138,139,1,0,0,0,139,140,1,0,0,0,140,142,5,
10,0,0,141,138,1,0,0,0,142,143,1,0,0,0,143,141,1,0,0,0,143,144,1,
0,0,0,144,145,1,0,0,0,145,146,6,17,4,0,146,40,1,0,0,0,147,149,7,
2,0,0,148,147,1,0,0,0,149,150,1,0,0,0,150,148,1,0,0,0,150,151,1,
0,0,0,151,42,1,0,0,0,152,154,7,0,0,0,153,152,1,0,0,0,154,155,1,0,
0,0,155,153,1,0,0,0,155,156,1,0,0,0,156,44,1,0,0,0,157,159,7,1,0,
0,158,157,1,0,0,0,159,160,1,0,0,0,160,158,1,0,0,0,160,161,1,0,0,
0,161,162,1,0,0,0,162,163,6,20,3,0,163,46,1,0,0,0,164,165,5,58,0,
0,165,166,1,0,0,0,166,167,6,21,4,0,167,168,6,21,2,0,168,48,1,0,0,
0,169,171,5,32,0,0,170,169,1,0,0,0,171,172,1,0,0,0,172,170,1,0,0,
0,172,173,1,0,0,0,173,174,1,0,0,0,174,175,6,22,4,0,175,176,6,22,
5,0,176,50,1,0,0,0,177,179,8,3,0,0,178,177,1,0,0,0,179,180,1,0,0,
0,180,178,1,0,0,0,180,181,1,0,0,0,181,182,1,0,0,0,182,183,6,23,4,
0,183,52,1,0,0,0,16,0,1,2,3,4,109,123,128,133,138,143,150,155,160,
172,180,6,5,3,0,5,2,0,5,1,0,6,0,0,4,0,0,5,4,0
]

class ObjectFileLexer(Lexer):
Expand Down Expand Up @@ -101,34 +102,35 @@ class ObjectFileLexer(Lexer):
ABS_SECTION = 12
COLON = 13
MINUS = 14
NEWLINE = 15
WS = 16
NEWLINE_BYTES = 17
BYTES = 18
WORD_ABS = 19
WS_ABS = 20
COLON_ABS = 21
SPACES_FILE = 22
FILEPATH = 23
PLUS = 15
NEWLINE = 16
WS = 17
NEWLINE_BYTES = 18
BYTES = 19
WORD_ABS = 20
WS_ABS = 21
COLON_ABS = 22
SPACES_FILE = 23
FILEPATH = 24

channelNames = [ u"DEFAULT_TOKEN_CHANNEL", u"HIDDEN" ]

modeNames = [ "DEFAULT_MODE", "IN_BYTES", "IN_ABS", "IN_FILE", "IN_FILEPATH" ]

literalNames = [ "<INVALID>",
"'TARG'", "'FILE'", "'ABS'", "'LOC'", "'NTRY'", "'NAME'", "'ALIG'",
"'DATA'", "'REL'", "'XTRN'", "'$abs'", "'-'" ]
"'DATA'", "'REL'", "'XTRN'", "'$abs'", "'-'", "'+'" ]

symbolicNames = [ "<INVALID>",
"TARG", "FILE", "ABS", "LOC", "NTRY", "NAME", "ALIG", "DATA",
"REL", "XTRN", "WORD", "ABS_SECTION", "COLON", "MINUS", "NEWLINE",
"WS", "NEWLINE_BYTES", "BYTES", "WORD_ABS", "WS_ABS", "COLON_ABS",
"SPACES_FILE", "FILEPATH" ]
"REL", "XTRN", "WORD", "ABS_SECTION", "COLON", "MINUS", "PLUS",
"NEWLINE", "WS", "NEWLINE_BYTES", "BYTES", "WORD_ABS", "WS_ABS",
"COLON_ABS", "SPACES_FILE", "FILEPATH" ]

ruleNames = [ "TARG", "FILE", "ABS", "LOC", "NTRY", "NAME", "ALIG",
"DATA", "REL", "XTRN", "WORD", "ABS_SECTION", "COLON",
"MINUS", "NEWLINE", "WS", "NEWLINE_BYTES", "BYTES", "WORD_ABS",
"WS_ABS", "COLON_ABS", "SPACES_FILE", "FILEPATH" ]
"MINUS", "PLUS", "NEWLINE", "WS", "NEWLINE_BYTES", "BYTES",
"WORD_ABS", "WS_ABS", "COLON_ABS", "SPACES_FILE", "FILEPATH" ]

grammarFileName = "ObjectFileLexer.g4"

Expand Down
Loading
Loading