]> git.the-white-hart.net Git - vhdl/commitdiff
Add local labels and error on label redefinition
authorrs <>
Fri, 10 Oct 2025 02:05:17 +0000 (21:05 -0500)
committerrs <>
Fri, 10 Oct 2025 02:05:17 +0000 (21:05 -0500)
projects/cpu_0/asm/as.py

index 88c4a1e8764e7ac452f18f21c85c869ec0d10c17..c4d2892dff0e5c6365bf4a6ae9b79b0e9c838226 100755 (executable)
@@ -41,6 +41,7 @@ class Assembler(object):
     def chomp(self, filename: str, source: str) -> bool:
         error = False
         comment = False
+        scope_label = ''
         for token, line_no in self.tokenize(source):
             if comment:
                 if token == ')':
@@ -56,14 +57,25 @@ class Assembler(object):
             elif token.lower() in self.INSTRS:
                 self.flash_section += self.INSTRS[token.lower()]
             elif '=' in token:
-                sym, value = token.split('=', maxsplit=1)
+                label, value = token.split('=', maxsplit=1)
                 value = int(value, 0)
-                self.syms[sym] = value
+                if label in self.syms:
+                    print(f'{filename}:{line_no} - "{label}" redefined')
+                    error = True
+                self.syms[label] = value
             elif token.endswith(':'):
-                if token[0] not in '_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ':
-                    print(f'{filename}:{line_no} - Invalid label name "{token}"')
+                label = token[:-1]
+                if label[0] not in '._abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ':
+                    print(f'{filename}:{line_no} - Invalid label name "{label}"')
+                    error = True
+                if label[0] != '.':
+                    scope_label = label
+                else:
+                    label = scope_label + label
+                if label in self.syms:
+                    print(f'{filename}:{line_no} - "{label}" redefined')
                     error = True
-                self.syms[token[:-1]] = len(self.flash_section)
+                self.syms[label] = len(self.flash_section)
             elif token[0] in '-0123456789':
                 if token.endswith('i8'):
                     val = int(token[:-2], 0)
@@ -83,7 +95,9 @@ class Assembler(object):
                 else:
                     print(f'{filename}:{line_no} - Number "{token}" must end with "i8" or "i32"')
                     error = True
-            elif token[0] in '_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ':
+            elif token[0] in '._abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ':
+                if token[0] == '.':
+                    token = scope_label + token
                 if token in self.syms:
                     self.flash_section += struct.pack('<I', self.syms[token])
                 else: