#!/usr/bin/env python3 """Assemble test programs with the monitor's assembler (out/monitor-host) and with GNU as, and check the bytes are the same: one in GNU syntax, given to both as it is, and one in the older forms the monitor still takes. python3 test-asm.py """ import os import re import subprocess import sys import tempfile HERE = os.path.dirname(os.path.abspath(__file__)) # GNU syntax: both assemblers read this text (GNU as without the .org) GNU_SRC = r""" .org 0x12000 .text .option norvc .globl _start .equ count, 10 .set mask, 0b1010 << 4 | 1 # 161 width = (count * 3) ^ ~0xf # mixed operators big = 0x12345 _start: la a1, message # auipc and addi li a2, count li a3, 0x12000 # lui alone li a4, 0x12345 # lui and addi li a5, -1 li t0, mask li t1, width & 0x7ff lui a6, %hi(big) addi a6, a6, %lo(big) lui a7, %hi(table) lw a7, %lo(table)(a7) sw zero, %lo(table)(a7) print_char: lbu a0, 0(a1) beqz a0, finish wait: lbu t0, -256(zero) andi t0, t0, 2 beqz t0, wait sb a0, -255(zero) addi a1, a1, 1 j print_char finish: li a0, '\n' li a1, 'Z' - 1 csrr a2, mstatus csrw mscratch, a2 csrrs a3, cycle, zero frrm a4 fadd.s fa0, fa1, fa2, rtz fmadd.s ft0, ft1, ft2, ft3 flw fs0, 4(sp) fsw ft11, -8(sp) fcvt.w.s t2, fa0, rtz jal print_char jalr t0 ret .align 4 # NOPs from a word message: .string "hello, \"world\"\n", "two" .ascii "tab\there" .asciz "back\\slash" .p2align 2, 0 # GNU leaves bytes unaligned without a fill table: .word _start, finish, . - _start, 0x12345678 .long -1 .4byte 'A' .half 1234, 0xffff .short 7 .2byte -2 .byte 1, 2, 0x7f, 'X', '\t' # (GNU reads '\0' as '0') .space 3, 0xaa .zero 2 .skip 1 .balign 8 .byte 0xee """ # the older forms; to_gnu() below turns them into GNU as's SRC = r""" ORG 12000H COUNT EQU 10 START: LA A1,MSG ; the message LI A2,COUNT LI A3,BIG ; defined further on: LUI and ADDI LI T0,-2048 LI T1,2047 LOOP: LBU A0,0(A1) BEQZ A0,DONE WAIT: LBU T0,-256(ZERO) ANDI T0,T0,2 BEQZ T0,WAIT SB A0,-255(ZERO) ADDI A1,A1,1 J LOOP DONE: ADDI A2,A2,-1 BNEZ A2,START MUL A4,A2,A3 DIVU A5,A4,A3 REMU A6,A4,A3 SLLI A7,A6,3 SRAI S2,A7,1 SUB S3,S2,A7 NEG S4,S3 NOT S5,S4 SEQZ S6,S5 SNEZ S7,S5 BGT S6,S7,DONE BLEU S6,S7,LOOP BGTZ S6,LOOP BLEZ S6,LOOP JAL RA,DONE JAL DONE JALR T0 JALR RA,4(T0) JR T0 CSRR A0,MSTATUS CSRW MSCRATCH,A0 CSRSI MSTATUS,8 CSRRW A1,0x305,A2 LUI S8,0x12345 AUIPC S9,1 SW S9,-4(SP) SH S9,2(SP) LH S10,2(SP) LHU S11,2(SP) LW S11,0(SP) ECALL EBREAK MRET WFI FENCE FENCE.I NOP FLW FA0,8(SP) FSW F31,-12(A0) FADD.S FA0,FA1,FA2 FSUB.S F1,F2,F3,RTZ FMUL.S FT0,FT11,FS0 FDIV.S FS11,FS1,FS2,RMM FSQRT.S F4,F5,DYN FMADD.S F1,F2,F3,F4 FNMSUB.S F1,F2,F3,F4,RUP FSGNJX.S F6,F7,F8 FMV.S F9,F10 FNEG.S F9,F10 FABS.S F9,F10 FMIN.S F11,F12,F13 FMAX.S F11,F12,F13 FEQ.S A0,F1,F2 FLT.S A1,F1,F2 FLE.S A2,F1,F2 FCVT.W.S A3,F4,RTZ FCVT.WU.S A4,F4 FCVT.S.W F5,A5 FCVT.S.WU F6,A6,RDN FMV.X.W T0,F7 FMV.W.X F8,T1 FCLASS.S T2,F9 CSRR A0,FCSR CSRW FRM,A1 RET MSG: DB "HI",13,10,0 ALIGN 4 TAB: DW START,DONE,0x12345678 DH 1234,0xFFFF DS 6 DB 'A' BIG EQU 0x12345 """ def to_gnu(src): out = [".text", ".option norelax", ".option norvc", "_start:"] for line in src.strip().splitlines(): line = line.split(";")[0] line = line.replace("$", ".") m = re.match(r"(\w+)\s+EQU\s+(.*)", line.strip()) if m: out.append(".equ %s, %s" % (m.group(1).lower(), m.group(2).lower())) continue if line.strip().startswith("ORG"): continue line = re.sub(r'\bDB "(\w+)",', r'.ascii "\1"\n .byte ', line) line = re.sub(r"\bDB\b", ".byte", line) line = re.sub(r"\bDH\b", ".half", line) line = re.sub(r"\bDW\b", ".word", line) line = re.sub(r"\bDS\s+(\d+)", r".skip \1", line) line = re.sub(r"\bALIGN (\d+)", r".balign \1, 0", line) line = re.sub(r"\bJAL DONE\b", "jal ra,DONE", line) line = re.sub(r"\bJALR T0\b", "jalr ra,0(t0)", line) line = re.sub(r"\bLI A3,BIG\b", "lui a3,%hi(BIG)\n addi a3,a3,%lo(BIG)", line) out.append(line.lower().replace("'a'", "'A'").replace('"hi"', '"HI"')) return "\n".join(out) + "\n" def assemble_gnu(pre, text, tmp): open(os.path.join(tmp, "t.s"), "w").write(text) subprocess.run([pre + "as", "-march=rv32imf_zicsr_zifencei", "-mno-relax", "-o", os.path.join(tmp, "t.o"), os.path.join(tmp, "t.s")], check=True) subprocess.run([pre + "ld", "-m", "elf32lriscv", "--no-relax", "-e", "0x12000", "-Ttext=0x12000", "-o", os.path.join(tmp, "t.elf"), os.path.join(tmp, "t.o")], check=True) subprocess.run([pre + "objcopy", "-O", "binary", os.path.join(tmp, "t.elf"), os.path.join(tmp, "t.bin")], check=True) return open(os.path.join(tmp, "t.bin"), "rb").read().hex().upper() def compare(what, src, gnu_text, pre): ours = subprocess.run([os.path.join(HERE, "out", "monitor-host"), "asmbin"], input=src, capture_output=True, text=True).stdout.strip() if not re.fullmatch(r"[0-9A-F]{5} [0-9A-F]+", ours): print("FAIL %s: the monitor's assembler says %s" % (what, ours)) return False start, ours = ours.split() gnu = assemble_gnu(pre, gnu_text, tempfile.mkdtemp()) if 0 < len(gnu) - len(ours) < 32: # GNU pads the section's end to 16 bytes gnu = gnu[:len(ours)] ok = start == "12000" and ours == gnu if not ok: for i in range(0, max(len(ours), len(gnu)), 8): if ours[i:i + 8] != gnu[i:i + 8]: print("first difference at %05X: monitor %s, GNU %s" % (0x12000 + i // 2, ours[i:i + 8], gnu[i:i + 8])) break print("lengths: monitor %d bytes, GNU %d" % (len(ours) // 2, len(gnu) // 2)) print(("PASS" if ok else "FAIL") + " %s: %d bytes from 12000h the same as GNU as" % (what, len(ours) // 2)) return ok def host(cmd, text): return subprocess.run([os.path.join(HERE, "out", "monitor-host"), cmd], input=text, capture_output=True, text=True).stdout def main(): pre = subprocess.run(["bash", "-c", "source %s/../scripts/rv_env.sh; echo $RV_PREFIX" % HERE], capture_output=True, text=True).stdout.strip() ok = compare("GNU syntax", GNU_SRC, GNU_SRC.replace(".org 0x12000", ""), pre) ok &= compare("the older forms", SRC, to_gnu(SRC), pre) # the same lines as BASIC stores them, in capitals outside double quotes # (BASIC knows nothing of \" in a string, so that line is left out), and # with ; comments plain = "\n".join(l for l in GNU_SRC.splitlines() if '\\"' not in l) upper = re.sub(r'("[^"]*")|[a-z]', lambda m: m.group(1) or m.group(0).upper(), plain) upper = upper.replace("#", ";") want = host("asmbin", plain) same = host("asmbin", upper) == want and "ERROR" not in want print(("PASS" if same else "FAIL") + " the same in capitals, with ; comments") ok &= same # errors errs = {".align 33": "ALIGN", ".equ 5": "NAME", "li a0, %mid(5)": "BAD EXPRESSION", ".ascii 5": "BAD STRING", "lw a0, 0(q9)": "REGISTER", ".byte 0b102": "BAD NUMBER"} bad = [k for k, v in errs.items() if v not in host("asm", k + "\n")] print(("PASS" if not bad else "FAIL %s" % bad) + " errors") sys.exit(0 if ok and not bad else 1) if __name__ == "__main__": main()