aboutsummaryrefslogtreecommitdiffstats
path: root/src/asm_pass1.py
blob: d46252613c190eca19e9971c3601c4307de187d6 (plain)
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
import importlib
import sys
import os.path
from dataclasses import dataclass
from asm_ops import *
from asm_eval import *
from asm_pass0 import *

@dataclass
class Result1Line:
  line_num: int
  src_file: str
  parts: list[str]

@dataclass
class Result1:
  lines: list[Result1Line]
  options: Options
  operations: OpExpansionDict

def pass_1(result0: Result0) -> Result1:
  options = Options()
  result_lines: list[Result1Line] = []
  operations: OpExpansionDict = default_expansions.copy()

  for line in result0.lines:
    keyword, *args = line.line.lower().split()
    match keyword:
      case "@opt":
        opt_name, opt_value = args
        match opt_name:
          case "stack_pointer": options.stack_pointer = opt_value
          case "csr_scratch": options.csr_scratch = opt_value
          case _: raise Exception("Unknown @opt: " + opt_name)
      case "@use":
        module_name, ops = args[0].split(":")
        ops_split = ops.split(",")
        sys.path.append(os.path.dirname(line.src_file))
        module = importlib.import_module(module_name)
        mod_expansions: OpExpansionDict = module.expansions
        for op in mod_expansions:
          expansion = mod_expansions[op]
          if ops == "*" or op in ops_split:
            operations[op] = expansion
      case _:
        result_lines.append(Result1Line(
          src_file=line.src_file,
          line_num=line.line_num,
          parts=[keyword, *args]
        ))

  return Result1(
    operations=operations,
    options=options,
    lines=result_lines
  )