Add partial support for 8-bit loads
We're big-endian. when we have this in memory: 00 01 02 03 And we need the first byte, we load the first 16-bit word: 0x0001 The first byte is then in the upper part of the word, and requires a right shift by 8. So any load into an 8-bit typed container needs to shift stuff. So far, stores from/to the stack are exempted, they always load/store full 16-bit words. And a few othe rminor things. Like string null terminators. And escaped characters in character literals. Can you believe it's spelled 'literal', with a single t? Me neither.
This commit is contained in:
@@ -145,7 +145,7 @@ struct_field: type IDENTIFIER sized_array* ";"
|
||||
IDENTIFIER: /[_a-zA-Z]\w*/
|
||||
COMMENT: /\/\/.*/
|
||||
HEX_LITTERAL: /0x[a-fA-F0-9]+/
|
||||
CHARACTER: /'[^']'/
|
||||
CHARACTER: /'([^']|\\n|\\r|\\0|\\t)'/
|
||||
|
||||
|
||||
%import common.WS
|
||||
|
23
tools/cc.py
23
tools/cc.py
@@ -366,7 +366,7 @@ class CcTransform(lark.visitors.Transformer):
|
||||
mul = _binary_op(lambda a, b: a*b)
|
||||
shl = _binary_op(lambda a, b: a<<b)
|
||||
|
||||
CHARACTER = lambda _, x: ord(x[1])
|
||||
CHARACTER = lambda _, x: ord(ast.literal_eval(x)[0])
|
||||
IDENTIFIER = str
|
||||
SIGNED_NUMBER = int
|
||||
HEX_LITTERAL = lambda _, x: int(x[2:], 16)
|
||||
@@ -543,6 +543,20 @@ class Load(AsmOp):
|
||||
def __init__(self, fun, ops):
|
||||
self.fun = fun
|
||||
self.dest, self.var = ops
|
||||
fun.log(f'8bit load? {self.var.type} {self.is8bit}')
|
||||
|
||||
@property
|
||||
def is8bit(self):
|
||||
if not isinstance(self.var.type, PodType):
|
||||
return False
|
||||
if self.var.type.pod in ['uint8_t', 'int8_t', 'char']:
|
||||
return True
|
||||
return False
|
||||
|
||||
def _maybecast8bit(self, reg):
|
||||
if self.is8bit:
|
||||
return [f'shr {reg}, {reg}, 8']
|
||||
return []
|
||||
|
||||
@property
|
||||
def out(self):
|
||||
@@ -552,13 +566,13 @@ class Load(AsmOp):
|
||||
reg = self.dest
|
||||
if self.var.name in self.fun.locals:
|
||||
src = self.var.stackaddr
|
||||
return [f'load {reg}, [sp, {src}]']
|
||||
return [f'load {reg}, [sp, {src}]'] # no 8bit cast to/from stack
|
||||
elif self.var.addr_reg is not None:
|
||||
return [f'load {reg}, [{self.var.addr_reg}]']
|
||||
return [f'load {reg}, [{self.var.addr_reg}]'] + self._maybecast8bit(reg)
|
||||
else:
|
||||
return [f'set {reg}, {self.var.name}',
|
||||
f'nop // in case we load a far global',
|
||||
f'load {reg}, [{reg}]']
|
||||
f'load {reg}, [{reg}]'] # unsure if we need a cast here
|
||||
|
||||
class Store(AsmOp):
|
||||
scratch_need = 1
|
||||
@@ -1254,6 +1268,7 @@ def parse_tree(tree, debug=False):
|
||||
for sym, dat in inte.strings.items():
|
||||
out += [f'.global {sym}',
|
||||
f'{sym}:']
|
||||
dat += '\0'
|
||||
if len(dat) % 2 != 0:
|
||||
dat += '\0'
|
||||
dat = dat.encode()
|
||||
|
Reference in New Issue
Block a user