Skip to content

Commit

Permalink
inline assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
ibx34 committed Dec 26, 2024
1 parent f10e938 commit f7d3a32
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
3 changes: 1 addition & 2 deletions asm.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def __init__(
self.lines: list[str] = [
".global _start",
".p2align 3",
'helloworld: .ascii "Hello World!"',
]
self.symbol_tables: dict[int, SymbolTable] = symbol_tables
# All tables related to registers most likely
Expand Down Expand Up @@ -96,7 +95,7 @@ def generate(self, expr: AstirExpr | None = None) -> list[str]:
)
# to_add.append(f"// {asm_function.next_usable_reg}")
to_add.extend(self.generate(c_expr.right.body))
to_add.append("ret")
#to_add.append("ret")
self.inside_fn = None
elif isinstance(c_expr, ShuntingYardAlgorithmResults):
inside_fn = self.current_fn()
Expand Down
26 changes: 17 additions & 9 deletions ast_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def collect_until(
self,
check: Callable[[str | None, str], bool],
devance_b4_break: bool = False,
escape: bool = False,
start_str: str = "",
) -> str:
temp_str: str = start_str
Expand All @@ -85,6 +86,7 @@ def collect_until(
break
self.advance()
temp_str += c
print(f"--> {temp_str}")
return temp_str

def lex(self) -> Token:
Expand All @@ -101,7 +103,21 @@ def lex(self) -> Token:
return Token(TT.DOUBLE_COLON)
elif c == '"':
self.advance()
string = self.collect_until(lambda c, _: (c is None) or c == '"')
string = ""
while c := self.current():
if c is None or c == '"':
break
elif c == "\\":
self.advance()
c = self.current()
if c is None:
break
elif c == '"':
string += '"'
self.advance()
continue
self.advance()
string += c
return Token(TT.LITERAL, prim_ty=PrimitiveTypes.STR, val=string)
elif c not in TT and (is_valid_ident(c) or c == "."):
self.advance()
Expand Down Expand Up @@ -377,11 +393,6 @@ def parse(self, tag=None) -> AstirExpr | None:
if expr is not None and isinstance(expr, Reference):
sym_table.insert(c.val, expr)
result = Parameter()
# elif next.ty is TT.OPEN_PAREN:
# self.advance()
# paren = self.parse()
# # raise Exception(f"Enum value(?): {c.val} -> {paren}")
# result = DataVariantWithInnerValue(Identifier(c.val), paren)
else:
self.advance()
for_assignment = False
Expand Down Expand Up @@ -528,9 +539,6 @@ def parse(self, tag=None) -> AstirExpr | None:
possible_args: list[AstirExpr] = []

for k, ref in parameters.symbols.items():
print(
f"{bcolors.BOLD}{bcolors.OKGREEN}** Checking for argument {ref.name} with type: {ref.val}{bcolors.ENDC}"
)
if ref.name == "ret":
continue

Expand Down
5 changes: 2 additions & 3 deletions boot.dal
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@

//take_int_and_add_2\ x int, y int :: int → x + y

asm ["print_hello_world: mov X0, #1", "adr X1, helloworld", "mov X2, #13", "mov X16, #4", "svc 0", "ret"]

_start\ :: int → asm ["bl print_hello_world", "mov X0, #0", "mov X16, #1", "svc 0"]
print_hello_world\ :: () → asm ["mov X0, #1", "adr X1, helloworld", "mov X2, #13", "mov X16, #4", "svc 0", "ret"]
_start\ :: int → asm ["bl print_hello_world", "mov X0, #0", "mov X16, #1", "svc 0", "helloworld: .ascii \"Hello World!\""]
1 change: 1 addition & 0 deletions boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

def run():
file = open("boot.dal").read()
print(file)
lexer = Lexer(file)
lexer.lex_all()
print(f"{lexer.results}\n\n")
Expand Down
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cd boot && ghc Main.hs -lm && ./main
python3 boot.py && as -o boot.o boot.s && ld -macosx_version_min 13.0.0 -o boot boot.o -lSystem -syslibroot `xcrun -sdk macosx --show-sdk-path` -e _start -arch arm64 && ./boot

0 comments on commit f7d3a32

Please sign in to comment.