Kaynağa Gözat

Fixed risc compiler

Min 5 yıl önce
ebeveyn
işleme
5cfd2530b4
3 değiştirilmiş dosya ile 105 ekleme ve 94 silme
  1. 14 4
      tools/asm_compiler.py
  2. 2 2
      tools/oisc8asm.py
  3. 89 88
      tools/risc8asm.py

+ 14 - 4
tools/asm_compiler.py

@@ -308,6 +308,7 @@ class Compiler:
         else:
             args = line_args[1]
         instr_name = line_args[0].lower()
+        instr_nameCS = line_args[0]
 
         # Builtin instructions
         if instr_name == 'db':
@@ -361,8 +362,12 @@ class Compiler:
                     raise CompilingError(f"Previous error")
             return scope
 
-        if scope + '.' + instr_name in self.labels:
-            instr_name = self.labels[scope + '.' + instr_name]  # replace with definition
+        dscope = scope + '.' if scope else ''
+        if dscope + instr_name in self.labels:
+            instr_name = self.labels[dscope + instr_name]  # replace with definition
+
+        if dscope + instr_nameCS in self.labels:
+            instr_name = self.labels[dscope + instr_nameCS]  # replace with definition
 
         if instr_name not in self.instr_db:
             raise CompilingError(f"Instruction '{instr_name}' not recognised!")
@@ -370,8 +375,11 @@ class Compiler:
         # replace args with %def
         ops = args2operands(args)
         for i, arg in enumerate(ops):
-            if scope + '.' + arg in self.labels:
-                ops[i] = self.labels[scope + '.' + arg]
+            if dscope + arg in self.labels:
+                val = self.labels[dscope + arg]
+                if isinstance(val, bytes):
+                    val = '0x' + val.hex()
+                ops[i] = val
         args = ','.join(ops)
 
         instr_obj = self.instr_db[instr_name.lower()]
@@ -440,6 +448,8 @@ class Compiler:
                                     s.depth = int(val)
                                 if key == 'width':
                                     s.width = int(val)
+                                if key == 'length':
+                                    s.length = int(val)
                             # m = secs_re.match(line_args[2])
                             # if m is not None:
                             #     g = m.groups()

+ 2 - 2
tools/oisc8asm.py

@@ -137,8 +137,8 @@ asmc.add_instr(InstructionDest("STACK", 5))
 asmc.add_instr(InstructionDest("MEM0", 6, alias=["MEMPT0"]))
 asmc.add_instr(InstructionDest("MEM1", 7, alias=["MEMPT1"]))
 asmc.add_instr(InstructionDest("MEM2", 8, alias=["MEMPT2"]))
-asmc.add_instr(InstructionDest("SWHI", 9, alias=["MEMSWHI", "MEMHI"]))
-asmc.add_instr(InstructionDest("SWLO", 10, alias=["MEMSWLO", "MEMLO"]))
+asmc.add_instr(InstructionDest("MEMHI", 9, alias=["MEMSWHI", "SWHI"]))
+asmc.add_instr(InstructionDest("MEMLO", 10, alias=["MEMSWLO", "SWLO"]))
 asmc.add_instr(InstructionDest("COMA", 11))
 asmc.add_instr(InstructionDest("COMD", 12))
 asmc.add_instr(InstructionDest("REG0", 13))

+ 89 - 88
tools/risc8asm.py

@@ -73,91 +73,92 @@ asmc.add_instr(compiler.Instruction('RJUMP', '1111_1100', 2))
 asmc.add_instr(compiler.Instruction('RBWI ', '1111_1101'))
 
 if __name__ == '__main__':
-    parser = argparse.ArgumentParser(description='Assembly compiler', add_help=True)
-    parser.add_argument('file', help='Files to compile')
-    parser.add_argument('-t', '--output_type', choices=['bin', 'mem', 'binary', 'mif', 'uhex'], default='mem',
-                        help='Output type')
-    parser.add_argument('-S', '--slice', default=-1, type=int, help='Slice output for section')
-    parser.add_argument('-o', '--output', help='Output directory')
-    parser.add_argument('-f', '--force', action='store_true', help='Force override output file')
-    parser.add_argument('-s', '--stdout', action='store_true', help='Print to stdout')
-    parser.add_argument('-D', '--decompile', action='store_true', help='Print decompiled')
-    parser.add_argument('section', help='Section')
-    args = parser.parse_args(sys.argv[1:])
-    if not path.isfile(args.file):
-        print(f'No file {args.file}!')
-        sys.exit(1)
-
-    output_dir = args.output or path.dirname(args.file)
-    if not path.exists(output_dir):
-        mkdir(output_dir)
-
-    if args.output_type == 'mem':
-        ext = '.mem'
-    elif args.output_type == 'bin':
-        ext = '.bin'
-    elif args.output_type == 'mif':
-        ext = '.mif'
-    elif args.output_type == 'uhex':
-        ext = '.uhex'
-    else:
-        ext = '.out'
-    bname = path.basename(args.file).rsplit('.', 1)[0]
-
-    sformat = f'01d'
-    outputs = []
-    if args.slice > 0:
-        sformat = f'0{int(math.log10(args.slice)) + 1}d'
-        for i in range(0, args.slice):
-            outputs.append(path.join(output_dir,f'{bname}{args.section}_{format(i, sformat)}{ext}'))
-    else:
-        outputs = [path.join(output_dir, bname + args.section + ext)]
-    if not args.stdout and not args.force:
-        for output in outputs:
-            if path.isfile(output):
-                print(f'Output file already exists {output}!')
-                sys.exit(1)
-
-    data = asmc.compile_file(args.file)
-    if data is not None:
-        section = args.section
-        if section in data:
-            width, length, size, bdata = data[section]
-            asize = len(bdata)
-            if size > 0:
-                bdataf = bdata + (size - len(bdata)) * bytearray(b'\x00')
-            else:
-                bdataf = bdata
-
-            for i, output in enumerate(outputs):
-
-                y = bdataf[i::len(outputs)]
-                if args.output_type == 'binary':
-                    x = compiler.convert_to_binary(y)
-                elif args.output_type == 'mem':
-                    x = compiler.convert_to_mem(y, width=width)
-                elif args.output_type == 'mif':
-                    x = compiler.convert_to_mif(y, width=width, depth=len(y)/width)
-                elif args.output_type == 'uhex':
-                    x = compiler.convert_to_mem(y, width=width, uhex=True)
-                else:
-                    x = bytes(y)
-
-                op = 'Printing' if args.stdout else 'Saving'
-                print(f"{op} {args.output_type} {section} data '{output}' [Size: {len(y)}B Slice: {format(i + 1, sformat)}/{len(outputs)}]")
-                if args.stdout:
-                    if args.decompile:
-                        print(asmc.decompile(bdata))
-                    else:
-                        print(x.decode())
-                else:
-                    with open(output, 'wb') as of:
-                        of.write(x)
-
-            print(f"Total {section} size: {len(bdata)/len(bdataf)*100:.1f}% [{len(bdata)}B/{len(bdataf)}B]")
-        else:
-            print(f'No such section {section}!')
-    else:
-        print(f'Failed to compile {args.file}!')
-        sys.exit(1)
-    sys.exit(0)
+    compiler.main(asmc)
+    # parser = argparse.ArgumentParser(description='Assembly compiler', add_help=True)
+    # parser.add_argument('file', help='Files to compile')
+    # parser.add_argument('-t', '--output_type', choices=['bin', 'mem', 'binary', 'mif', 'uhex'], default='mem',
+    #                     help='Output type')
+    # parser.add_argument('-S', '--slice', default=-1, type=int, help='Slice output for section')
+    # parser.add_argument('-o', '--output', help='Output directory')
+    # parser.add_argument('-f', '--force', action='store_true', help='Force override output file')
+    # parser.add_argument('-s', '--stdout', action='store_true', help='Print to stdout')
+    # parser.add_argument('-D', '--decompile', action='store_true', help='Print decompiled')
+    # parser.add_argument('section', help='Section')
+    # args = parser.parse_args(sys.argv[1:])
+    # if not path.isfile(args.file):
+    #     print(f'No file {args.file}!')
+    #     sys.exit(1)
+    #
+    # output_dir = args.output or path.dirname(args.file)
+    # if not path.exists(output_dir):
+    #     mkdir(output_dir)
+    #
+    # if args.output_type == 'mem':
+    #     ext = '.mem'
+    # elif args.output_type == 'bin':
+    #     ext = '.bin'
+    # elif args.output_type == 'mif':
+    #     ext = '.mif'
+    # elif args.output_type == 'uhex':
+    #     ext = '.uhex'
+    # else:
+    #     ext = '.out'
+    # bname = path.basename(args.file).rsplit('.', 1)[0]
+    #
+    # sformat = f'01d'
+    # outputs = []
+    # if args.slice > 0:
+    #     sformat = f'0{int(math.log10(args.slice)) + 1}d'
+    #     for i in range(0, args.slice):
+    #         outputs.append(path.join(output_dir,f'{bname}{args.section}_{format(i, sformat)}{ext}'))
+    # else:
+    #     outputs = [path.join(output_dir, bname + args.section + ext)]
+    # if not args.stdout and not args.force:
+    #     for output in outputs:
+    #         if path.isfile(output):
+    #             print(f'Output file already exists {output}!')
+    #             sys.exit(1)
+    #
+    # data = asmc.compile_file(args.file)
+    # if data is not None:
+    #     section = args.section
+    #     if section in data:
+    #         width, length, size, bdata = data[section]
+    #         asize = len(bdata)
+    #         if size > 0:
+    #             bdataf = bdata + (size - len(bdata)) * bytearray(b'\x00')
+    #         else:
+    #             bdataf = bdata
+    #
+    #         for i, output in enumerate(outputs):
+    #
+    #             y = bdataf[i::len(outputs)]
+    #             if args.output_type == 'binary':
+    #                 x = compiler.convert_to_binary(y)
+    #             elif args.output_type == 'mem':
+    #                 x = compiler.convert_to_mem(y, width=width)
+    #             elif args.output_type == 'mif':
+    #                 x = compiler.convert_to_mif(y, width=width, depth=len(y)/width)
+    #             elif args.output_type == 'uhex':
+    #                 x = compiler.convert_to_mem(y, width=width, uhex=True)
+    #             else:
+    #                 x = bytes(y)
+    #
+    #             op = 'Printing' if args.stdout else 'Saving'
+    #             print(f"{op} {args.output_type} {section} data '{output}' [Size: {len(y)}B Slice: {format(i + 1, sformat)}/{len(outputs)}]")
+    #             if args.stdout:
+    #                 if args.decompile:
+    #                     print(asmc.decompile(bdata))
+    #                 else:
+    #                     print(x.decode())
+    #             else:
+    #                 with open(output, 'wb') as of:
+    #                     of.write(x)
+    #
+    #         print(f"Total {section} size: {len(bdata)/len(bdataf)*100:.1f}% [{len(bdata)}B/{len(bdataf)}B]")
+    #     else:
+    #         print(f'No such section {section}!')
+    # else:
+    #     print(f'Failed to compile {args.file}!')
+    #     sys.exit(1)
+    # sys.exit(0)