|
309 | 309 |
|
310 | 310 | # -------------------- FPU helpers --------------------
|
311 | 311 |
|
| 312 | +_cached_opcodes = None |
| 313 | + |
| 314 | + |
| 315 | +def load_opcodes(): |
| 316 | + global _cached_opcodes |
| 317 | + opcode_file_name = 'opcodes-flt-occamy_CUSTOM.csv' |
| 318 | + opcode_file_path = pathlib.Path(__file__).parent.absolute() / opcode_file_name |
| 319 | + |
| 320 | + _cached_opcodes = {} |
| 321 | + with open(opcode_file_path, 'r') as f: |
| 322 | + for line in f: |
| 323 | + fields = line.strip().split(',') |
| 324 | + insn_name = fields[0] |
| 325 | + vec_params = fields[1:5] |
| 326 | + _cached_opcodes[insn_name] = vec_params |
| 327 | + |
312 | 328 |
|
313 | 329 | def vec_formatter(insn: str, op_type: str, hex_val: int, fmt: int) -> str:
|
314 |
| - # file data: |
315 |
| - # instruction,source_width,source_vec_len,destination_width,destination_vec_len |
316 |
| - opcodes_file_name = 'opcodes-flt-occamy_CUSTOM.csv' |
317 |
| - opcodes_file_path = pathlib.Path(__file__).parent.absolute() / opcodes_file_name |
| 330 | + global _cached_opcodes |
| 331 | + if _cached_opcodes is None: |
| 332 | + load_opcodes() |
| 333 | + |
318 | 334 | # cut the insn after the first space
|
319 | 335 | insn = insn.split(' ')[0]
|
320 | 336 | # check if operand is a source or a destination
|
321 | 337 | is_rd = (op_type == 'rd')
|
322 |
| - # check if the insn is in the opcodes file |
323 |
| - with open(opcodes_file_path, 'r') as f: |
324 |
| - for line in f: |
325 |
| - if insn in line: |
326 |
| - vec_params = line.strip().split(',')[1:5] |
327 |
| - # check if vector support for the insn is implemented |
328 |
| - if vec_params != ([''] * 4): |
329 |
| - # decode vector |
330 |
| - if not is_rd: |
331 |
| - width, vec_len = map(int, vec_params[0:2]) |
332 |
| - else: |
333 |
| - width, vec_len = map(int, vec_params[2:4]) |
334 |
| - # divide the hex value into source_vec_len each of width source_width |
335 |
| - vec = reversed([hex_val >> (width * i) & (2**width - 1) for i in range(vec_len)]) |
336 |
| - # decode the source_vec |
337 |
| - return [flt_decode(val, fmt) for val in vec] |
338 |
| - else: |
339 |
| - # if vector instruction but vector formatting not supported, return hex |
340 |
| - return hex(hex_val) |
| 338 | + # check if the insn is in the opcodes file else return None |
| 339 | + vec_params = _cached_opcodes.get(insn, None) |
| 340 | + # check if vector support for the insn is implemented |
| 341 | + if vec_params != ([''] * 4) or vec_params[2] != '1' or not None: |
| 342 | + # decode vector |
| 343 | + if not is_rd: |
| 344 | + width, vec_len = map(int, vec_params[0:2]) |
| 345 | + else: |
| 346 | + width, vec_len = map(int, vec_params[2:4]) |
| 347 | + # divide the hex value into source_vec_len each of width source_width |
| 348 | + vec = reversed([hex_val >> (width * i) & (2**width - 1) for i in range(vec_len)]) |
| 349 | + # decode the source_vec |
| 350 | + return [flt_decode(val, fmt) for val in vec] |
| 351 | + else: |
| 352 | + # if vector instruction but vector formatting not supported, return hex |
| 353 | + return hex(hex_val) |
341 | 354 | # if not vector instruction, default to scalar behaviour
|
342 | 355 | return flt_lit(hex_val, fmt)
|
343 | 356 |
|
|
0 commit comments