4
4
5
5
__all__ = [
6
6
'R_instr' , 'I_instr' , 'S_instr' ,
7
- 'SB_instr' , 'U_instr' , 'UJ_instr' ,'pseudo_instr' ,
7
+ 'SB_instr' , 'U_instr' , 'UJ_instr' ,'pseudo_instr' , 'JUMP' ,
8
8
'R' , 'I' , 'S' , 'SB' , 'U' , 'UJ' ,
9
9
'Rp' , 'Ip' , 'Sp' , 'SBp' , 'Up' , 'UJp' , 'Psp' ]
10
10
@@ -69,7 +69,8 @@ def compute_instr(self, instr, rs1, imm, rd):
69
69
@staticmethod
70
70
def immediate (imm ):
71
71
#return int(imm) - ((int(imm)>>12)<<12) # imm[11:0]
72
- return format (int (imm ), '012b' )
72
+ return format (((1 << 12 ) - 1 ) & int (imm ), '012b' )
73
+ #return format(int(imm), '012b')
73
74
74
75
class _S (Instruction ):
75
76
def __repr__ (self ):
@@ -97,7 +98,7 @@ def immediate(imm, n):
97
98
mod_imm_2 = int(imm) - ((int(imm) >> 5) << 5) # imm[4:0]
98
99
99
100
return mod_imm, mod_imm_2'''
100
- mod_imm = format (((1 << 13 ) - 1 ) & int (imm ), '013b ' )
101
+ mod_imm = format (((1 << 12 ) - 1 ) & int (imm ), '012b ' )
101
102
if n == 1 :
102
103
return mod_imm [0 ] + mod_imm [12 - 10 : 12 - 4 ]
103
104
return mod_imm [12 - 4 : 12 - 0 ] + mod_imm [1 ]
@@ -127,8 +128,8 @@ def compute_instr(self, instr, rs1, rs2, imm):
127
128
def immediate (imm , n ):
128
129
mod_imm = format (((1 << 13 ) - 1 ) & int (imm ), '013b' )
129
130
if n == 1 :
130
- return mod_imm [12 - 12 ] + mod_imm [12 - 10 :12 - 5 ]
131
- return mod_imm [12 - 4 :12 - 1 ] + mod_imm [12 - 11 ]
131
+ return mod_imm [12 - 12 ] + mod_imm [12 - 11 :12 - 5 ]
132
+ return mod_imm [12 - 4 :12 - 0 ] + mod_imm [12 - 11 ]
132
133
133
134
class _U (Instruction ):
134
135
def __repr__ (self ):
@@ -201,10 +202,11 @@ def __str__(self):
201
202
return "I Parser"
202
203
203
204
def organize (self , tokens ):
205
+ line_num , code = tokens [- 2 ], tokens [- 1 ]
204
206
instr , rs1 , imm , rd = tokens [0 ], None , None , None
205
207
if instr == "jalr" :
206
208
if len (tokens ) == 4 :
207
- rs1 , imm , rd = reg_map [tokens [2 ]], JUMP (tokens [3 ]), reg_map [tokens [1 ]]
209
+ rs1 , imm , rd = reg_map [tokens [2 ]], JUMP (tokens [3 ], line_num , code ), reg_map [tokens [1 ]]
208
210
else :
209
211
rs1 , imm , rd = reg_map [tokens [1 ]], 0 , reg_map ["x1" ]
210
212
elif instr == "lw" :
@@ -235,7 +237,8 @@ def __str__(self):
235
237
return "SB Parser"
236
238
237
239
def organize (self , tokens ):
238
- instr , rs1 , rs2 , imm = tokens [0 ], reg_map [tokens [1 ]], reg_map [tokens [2 ]], JUMP (tokens [3 ])
240
+ line_num , code = tokens [- 2 ], tokens [- 1 ]
241
+ instr , rs1 , rs2 , imm = tokens [0 ], reg_map [tokens [1 ]], reg_map [tokens [2 ]], JUMP (tokens [3 ], line_num , code )
239
242
return SB (instr , rs1 , rs2 , imm )
240
243
241
244
class _U_parse (InstructionParser ):
@@ -259,11 +262,12 @@ def __str__(self):
259
262
return "UJ Parser"
260
263
261
264
def organize (self , tokens ):
265
+ line_num , code = tokens [- 2 ], tokens [- 1 ]
262
266
instr , imm , rd = tokens [0 ], None , None
263
267
if len (tokens ) == 3 :
264
- imm , rd = JUMP (tokens [2 ]), reg_map [tokens [1 ]]
268
+ imm , rd = JUMP (tokens [2 ], line_num , code ), reg_map [tokens [1 ]]
265
269
else :
266
- imm , rd = JUMP (tokens [1 ]), reg_map ["x1" ]
270
+ imm , rd = JUMP (tokens [1 ], line_num , code ), reg_map ["x1" ]
267
271
268
272
return UJ (instr , imm , rd )
269
273
@@ -292,27 +296,25 @@ def organize(self, tokens):
292
296
293
297
return BadInstructionError ()
294
298
295
- def JUMP (x : str , line_num : int ) -> int :
296
- raise NotImplementedError ()
297
-
299
+ def JUMP (x : str , line_num : int , code : list ) -> int :
298
300
# search forward
299
301
skip_labels = 0
300
- for i in range (line_num , len (self . code )):
301
- if x + ":" == self . code [i ]:
302
+ for i in range (line_num , len (code )):
303
+ if x + ":" == code [i ]:
302
304
jump_size = (i - line_num - skip_labels ) * 4 # how many instructions to jump ahead
303
305
return jump_size
304
306
305
- if self . code [i ][- 1 ] == ':' :
307
+ if code [i ][- 1 ] == ':' :
306
308
skip_labels += 1
307
309
308
310
# search backward
309
311
skip_labels = 0
310
312
for i in range (line_num , - 1 , - 1 ):
311
313
# substruct correct label itself
312
- if self . code [i ][- 1 ] == ':' :
314
+ if code [i ][- 1 ] == ':' :
313
315
skip_labels += 1
314
316
315
- if x + ":" == self . code [i ]:
317
+ if x + ":" == code [i ]:
316
318
jump_size = (i - line_num + skip_labels ) * 4 # how many instructions to jump behind
317
319
return jump_size
318
320
0 commit comments