aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xelf.py7
-rw-r--r--ex.elfbin8228 -> 8228 bytes
-rw-r--r--ex.format23
-rw-r--r--ex.pack16
-rw-r--r--ex.survey16
-rwxr-xr-xformat.py5
-rwxr-xr-xsurvey.py13
7 files changed, 62 insertions, 18 deletions
diff --git a/elf.py b/elf.py
index f0cc94e..0db1138 100755
--- a/elf.py
+++ b/elf.py
@@ -91,16 +91,17 @@ def elf(iter):
segments = []
segment = None
for _, line in iter:
- if line["type"] == "segment":
+ type = line["type"]
+ if type == "segment":
(name, addr) = line["segment"]
segment = {"name": name, "addr": addr, "content": []}
segments.append(segment)
- elif line["type"] == "instr":
+ elif type == "instr":
if segment == None:
raise ValueError("label or code outside of segment!")
segment["content"] += line["instr"]
- else:
+ elif type != "empty":
raise ValueError("elf input should contain only segments and data!")
c = write_elf_header(segments)
diff --git a/ex.elf b/ex.elf
index 19bb3e1..54d5804 100644
--- a/ex.elf
+++ b/ex.elf
Binary files differ
diff --git a/ex.format b/ex.format
index c9c68b8..5fb48d8 100644
--- a/ex.format
+++ b/ex.format
@@ -4,13 +4,20 @@ main:
# load 0x10010000 (UART0) into t0
37/7 05/5 10010/20
# store 0x48 (H) in UART0+0
-13/7 06/5 00/3 00/5 48/12
-23/7 00/5 02/3 05/5 06/5 00/7
+13/7 06/5 0/3 00/5 048/12
+23/7 00/5 2/3 05/5 06/5 00/7
# store 0x69 (i) in UART0+0
-13/7 06/5 00/3 00/5 69/12
-23/7 00/5 02/3 05/5 06/5 00/7
+13/7 06/5 0/3 00/5 069/12
+23/7 00/5 2/3 05/5 06/5 00/7
# store 0x0a (\n) in UART0+0
-13/7 06/5 00/3 00/5 0a/12
-23/7 00/5 02/3 05/5 06/5 00/7
-# jump back up to the top
-6f/7 00/5 main/8/off21>>12 main/1/off21>>11 main/10/off21>>1 main/1/off21>>20
+13/7 06/5 0/3 00/5 00a/12
+23/7 00/5 2/3 05/5 06/5 00/7
+# jump back up to the top (three versions):
+# a) jal 21bit relative (with 0th bit forced to 0)
+# 6f/jal 0/rd/x0 main/off21
+# b) auipc+jalr 32bit relative (note label offset)
+17/7 07/5 main/off32/[31:12]
+67/7 00/5 0/3 07/5 main+4/off32/[11:0]
+# c) lui+jalr 32bit absolute
+# 37/lui 7/rd/t2 main/imm32
+# 67/jalr 0/subop 0/rd/x0 7/rs/t2 main/imm32
diff --git a/ex.pack b/ex.pack
index 2bef2a1..5a6cdf4 100644
--- a/ex.pack
+++ b/ex.pack
@@ -1,9 +1,23 @@
== code 0x80000000
+# repeatedly print "Hi\n"
+# main:
+# load 0x10010000 (UART0) into t0
b7 02 01 10
+# store 0x48 (H) in UART0+0
13 03 80 04
23 a0 62 00
+# store 0x69 (i) in UART0+0
13 03 90 06
23 a0 62 00
+# store 0x0a (\n) in UART0+0
13 03 a0 00
23 a0 62 00
-6f f0 5f fe
+# jump back up to the top (three versions):
+# a) jal 21bit relative (with 0th bit forced to 0)
+# 6f/jal 0/rd/x0 main/off21
+# b) auipc+jalr 32bit relative (note label offset)
+97 f3 ff ff
+67 80 43 fe
+# c) lui+jalr 32bit absolute
+# 37/lui 7/rd/t2 main/imm32
+# 67/jalr 0/subop 0/rd/x0 7/rs/t2 main/imm32
diff --git a/ex.survey b/ex.survey
index c5f32d8..09e8475 100644
--- a/ex.survey
+++ b/ex.survey
@@ -1,9 +1,23 @@
== code 0x80000000
+# repeatedly print "Hi\n"
+# main:
+# load 0x10010000 (UART0) into t0
37/7 05/5 10010/20
+# store 0x48 (H) in UART0+0
13/7 06/5 00/3 00/5 48/12
23/7 00/5 02/3 05/5 06/5 00/7
+# store 0x69 (i) in UART0+0
13/7 06/5 00/3 00/5 69/12
23/7 00/5 02/3 05/5 06/5 00/7
+# store 0x0a (\n) in UART0+0
13/7 06/5 00/3 00/5 0a/12
23/7 00/5 02/3 05/5 06/5 00/7
-6f/7 00/5 ff/8 01/1 3f2/10 01/1
+# jump back up to the top (three versions):
+# a) jal 21bit relative (with 0th bit forced to 0)
+# 6f/jal 0/rd/x0 main/off21
+# b) auipc+jalr 32bit relative (note label offset)
+17/7 07/5 fffff/20
+67/7 00/5 00/3 07/5 fe4/12
+# c) lui+jalr 32bit absolute
+# 37/lui 7/rd/t2 main/imm32
+# 67/jalr 0/subop 0/rd/x0 7/rs/t2 main/imm32
diff --git a/format.py b/format.py
index 62c35b2..e75a188 100755
--- a/format.py
+++ b/format.py
@@ -148,10 +148,7 @@ def pack_i(instr):
rd = bits.u(subv.untag(rd, "rd"), 5)
rs = bits.u(subv.untag(rs, "rs"), 5)
if subv.is_reference(imm):
- if op_tag == "jalr":
- imm = bits.ref(imm, slice(12, 1))
- else:
- imm = bits.ref(imm, slice(11, 0))
+ imm = bits.ref(imm, slice(11, 0))
else:
imm = bits.i(subv.untag(imm, "imm12"), 12)
diff --git a/survey.py b/survey.py
index 3aceda7..a0246c9 100755
--- a/survey.py
+++ b/survey.py
@@ -31,19 +31,28 @@ Resolves label references and substitutes them with literal values.
... 6f/7 00/5 main/off21/[19:12] main/off21/[11:11] main/off21/[10:1] main/off21/[20:20]
... '''[1:-1]))))
== code 0x80000000
+# main:
+# load 0x10010000 (UART0) into t0
37/7 05/5 10010/20
+# store 0x48 (H) in UART0+0
13/7 06/5 00/3 00/5 48/12
23/7 00/5 02/3 05/5 06/5 00/7
+# store 0x65 (e) in UART0+0
13/7 06/5 00/3 00/5 65/12
23/7 00/5 02/3 05/5 06/5 00/7
+# store 0x6c (l) in UART0+0
13/7 06/5 00/3 00/5 6c/12
23/7 00/5 02/3 05/5 06/5 00/7
+# store 0x6c (l) in UART0+0
13/7 06/5 00/3 00/5 6c/12
23/7 00/5 02/3 05/5 06/5 00/7
+# store 0x6f (o) in UART0+0
13/7 06/5 00/3 00/5 6f/12
23/7 00/5 02/3 05/5 06/5 00/7
+# store 0x0a (\\n) in UART0+0
13/7 06/5 00/3 00/5 0a/12
23/7 00/5 02/3 05/5 06/5 00/7
+# jump back up to the top
6f/7 00/5 ff/8 1/1 3e6/10 1/1
"""
import subv
@@ -151,7 +160,9 @@ def survey(iter):
instr.append(observed)
line[type] = instr
yield subv.format(line)
- elif type == "segment":
+ elif type == "label":
+ yield "# {}".format(line["raw"])
+ else:
yield line["raw"]
except Exception as e:
msg = "failed to survey line"