git.s-ol.nu subv / 848e84b
fix jalr immediate slice, update example stage results s-ol 14 days ago
7 changed file(s) with 62 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
9090 segments = []
9191 segment = None
9292 for _, line in iter:
93 if line["type"] == "segment":
93 type = line["type"]
94 if type == "segment":
9495 (name, addr) = line["segment"]
9596 segment = {"name": name, "addr": addr, "content": []}
9697 segments.append(segment)
97 elif line["type"] == "instr":
98 elif type == "instr":
9899 if segment == None:
99100 raise ValueError("label or code outside of segment!")
100101
101102 segment["content"] += line["instr"]
102 else:
103 elif type != "empty":
103104 raise ValueError("elf input should contain only segments and data!")
104105
105106 c = write_elf_header(segments)
Binary diff not shown
33 # load 0x10010000 (UART0) into t0
44 37/7 05/5 10010/20
55 # store 0x48 (H) in UART0+0
6 13/7 06/5 00/3 00/5 48/12
7 23/7 00/5 02/3 05/5 06/5 00/7
6 13/7 06/5 0/3 00/5 048/12
7 23/7 00/5 2/3 05/5 06/5 00/7
88 # store 0x69 (i) in UART0+0
9 13/7 06/5 00/3 00/5 69/12
10 23/7 00/5 02/3 05/5 06/5 00/7
9 13/7 06/5 0/3 00/5 069/12
10 23/7 00/5 2/3 05/5 06/5 00/7
1111 # store 0x0a (\n) in UART0+0
12 13/7 06/5 00/3 00/5 0a/12
13 23/7 00/5 02/3 05/5 06/5 00/7
14 # jump back up to the top
15 6f/7 00/5 main/8/off21>>12 main/1/off21>>11 main/10/off21>>1 main/1/off21>>20
12 13/7 06/5 0/3 00/5 00a/12
13 23/7 00/5 2/3 05/5 06/5 00/7
14 # jump back up to the top (three versions):
15 # a) jal 21bit relative (with 0th bit forced to 0)
16 # 6f/jal 0/rd/x0 main/off21
17 # b) auipc+jalr 32bit relative (note label offset)
18 17/7 07/5 main/off32/[31:12]
19 67/7 00/5 0/3 07/5 main+4/off32/[11:0]
20 # c) lui+jalr 32bit absolute
21 # 37/lui 7/rd/t2 main/imm32
22 # 67/jalr 0/subop 0/rd/x0 7/rs/t2 main/imm32
00 == code 0x80000000
1 # repeatedly print "Hi\n"
2 # main:
3 # load 0x10010000 (UART0) into t0
14 b7 02 01 10
5 # store 0x48 (H) in UART0+0
26 13 03 80 04
37 23 a0 62 00
8 # store 0x69 (i) in UART0+0
49 13 03 90 06
510 23 a0 62 00
11 # store 0x0a (\n) in UART0+0
612 13 03 a0 00
713 23 a0 62 00
8 6f f0 5f fe
14 # jump back up to the top (three versions):
15 # a) jal 21bit relative (with 0th bit forced to 0)
16 # 6f/jal 0/rd/x0 main/off21
17 # b) auipc+jalr 32bit relative (note label offset)
18 97 f3 ff ff
19 67 80 43 fe
20 # c) lui+jalr 32bit absolute
21 # 37/lui 7/rd/t2 main/imm32
22 # 67/jalr 0/subop 0/rd/x0 7/rs/t2 main/imm32
00 == code 0x80000000
1 # repeatedly print "Hi\n"
2 # main:
3 # load 0x10010000 (UART0) into t0
14 37/7 05/5 10010/20
5 # store 0x48 (H) in UART0+0
26 13/7 06/5 00/3 00/5 48/12
37 23/7 00/5 02/3 05/5 06/5 00/7
8 # store 0x69 (i) in UART0+0
49 13/7 06/5 00/3 00/5 69/12
510 23/7 00/5 02/3 05/5 06/5 00/7
11 # store 0x0a (\n) in UART0+0
612 13/7 06/5 00/3 00/5 0a/12
713 23/7 00/5 02/3 05/5 06/5 00/7
8 6f/7 00/5 ff/8 01/1 3f2/10 01/1
14 # jump back up to the top (three versions):
15 # a) jal 21bit relative (with 0th bit forced to 0)
16 # 6f/jal 0/rd/x0 main/off21
17 # b) auipc+jalr 32bit relative (note label offset)
18 17/7 07/5 fffff/20
19 67/7 00/5 00/3 07/5 fe4/12
20 # c) lui+jalr 32bit absolute
21 # 37/lui 7/rd/t2 main/imm32
22 # 67/jalr 0/subop 0/rd/x0 7/rs/t2 main/imm32
147147 rd = bits.u(subv.untag(rd, "rd"), 5)
148148 rs = bits.u(subv.untag(rs, "rs"), 5)
149149 if subv.is_reference(imm):
150 if op_tag == "jalr":
151 imm = bits.ref(imm, slice(12, 1))
152 else:
153 imm = bits.ref(imm, slice(11, 0))
150 imm = bits.ref(imm, slice(11, 0))
154151 else:
155152 imm = bits.i(subv.untag(imm, "imm12"), 12)
156153
3030 ... 6f/7 00/5 main/off21/[19:12] main/off21/[11:11] main/off21/[10:1] main/off21/[20:20]
3131 ... '''[1:-1]))))
3232 == code 0x80000000
33 # main:
34 # load 0x10010000 (UART0) into t0
3335 37/7 05/5 10010/20
36 # store 0x48 (H) in UART0+0
3437 13/7 06/5 00/3 00/5 48/12
3538 23/7 00/5 02/3 05/5 06/5 00/7
39 # store 0x65 (e) in UART0+0
3640 13/7 06/5 00/3 00/5 65/12
3741 23/7 00/5 02/3 05/5 06/5 00/7
42 # store 0x6c (l) in UART0+0
3843 13/7 06/5 00/3 00/5 6c/12
3944 23/7 00/5 02/3 05/5 06/5 00/7
45 # store 0x6c (l) in UART0+0
4046 13/7 06/5 00/3 00/5 6c/12
4147 23/7 00/5 02/3 05/5 06/5 00/7
48 # store 0x6f (o) in UART0+0
4249 13/7 06/5 00/3 00/5 6f/12
4350 23/7 00/5 02/3 05/5 06/5 00/7
51 # store 0x0a (\\n) in UART0+0
4452 13/7 06/5 00/3 00/5 0a/12
4553 23/7 00/5 02/3 05/5 06/5 00/7
54 # jump back up to the top
4655 6f/7 00/5 ff/8 1/1 3e6/10 1/1
4756 """
4857 import subv
150159 instr.append(observed)
151160 line[type] = instr
152161 yield subv.format(line)
153 elif type == "segment":
162 elif type == "label":
163 yield "# {}".format(line["raw"])
164 else:
154165 yield line["raw"]
155166 except Exception as e:
156167 msg = "failed to survey line"