b0a1686f02ac7bc553024d7ead8d251a061ead3f
1 # SPDX-License-Identifier: LGPLv3+
2 # Copyright (C) 2021 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
3 # Funded by NLnet http://nlnet.nl
5 """SVP64 OpenPOWER v3.0B assembly translator
7 This class takes raw svp64 assembly mnemonics (aliases excluded) and creates
8 an EXT001-encoded "svp64 prefix" (as a .long) followed by a v3.0B opcode.
10 It is very simple and straightforward, the only weirdness being the
11 extraction of the register information and conversion to v3.0B numbering.
13 Encoding format of svp64: https://libre-soc.org/openpower/sv/svp64/
14 Encoding format of arithmetic: https://libre-soc.org/openpower/sv/normal/
15 Encoding format of LDST: https://libre-soc.org/openpower/sv/ldst/
16 **TODO format of branches: https://libre-soc.org/openpower/sv/branches/**
17 **TODO format of CRs: https://libre-soc.org/openpower/sv/cr_ops/**
18 Bugtracker: https://bugs.libre-soc.org/show_bug.cgi?id=578
23 from collections
import OrderedDict
25 from openpower
.decoder
.isa
.caller
import (SVP64PrefixFields
, SV64P_MAJOR_SIZE
,
26 SV64P_PID_SIZE
, SVP64RMFields
,
27 SVP64RM_EXTRA2_SPEC_SIZE
,
28 SVP64RM_EXTRA3_SPEC_SIZE
,
29 SVP64RM_MODE_SIZE
, SVP64RM_SMASK_SIZE
,
30 SVP64RM_MMODE_SIZE
, SVP64RM_MASK_SIZE
,
31 SVP64RM_SUBVL_SIZE
, SVP64RM_EWSRC_SIZE
,
33 from openpower
.decoder
.pseudo
.pagereader
import ISA
34 from openpower
.decoder
.power_svp64
import SVP64RM
, get_regtype
, decode_extra
35 from openpower
.decoder
.selectable_int
import SelectableInt
36 from openpower
.consts
import SVP64MODE
39 from openpower
.util
import log
42 # decode GPR into sv extra
43 def get_extra_gpr(etype
, regmode
, field
):
44 if regmode
== 'scalar':
45 # cut into 2-bits 5-bits SS FFFFF
47 field
= field
& 0b11111
49 # cut into 5-bits 2-bits FFFFF SS
50 sv_extra
= field
& 0b11
52 return sv_extra
, field
55 # decode 3-bit CR into sv extra
56 def get_extra_cr_3bit(etype
, regmode
, field
):
57 if regmode
== 'scalar':
58 # cut into 2-bits 3-bits SS FFF
62 # cut into 3-bits 4-bits FFF SSSS but will cut 2 zeros off later
63 sv_extra
= field
& 0b1111
65 return sv_extra
, field
69 def decode_subvl(encoding
):
70 pmap
= {'2': 0b01, '3': 0b10, '4': 0b11}
71 assert encoding
in pmap
, \
72 "encoding %s for SUBVL not recognised" % encoding
77 def decode_elwidth(encoding
):
78 pmap
= {'8': 0b11, '16': 0b10, '32': 0b01}
79 assert encoding
in pmap
, \
80 "encoding %s for elwidth not recognised" % encoding
84 # decodes predicate register encoding
85 def decode_predicate(encoding
):
96 'nl': (1, 0b001), 'ge': (1, 0b001), # same value
98 'ng': (1, 0b011), 'le': (1, 0b011), # same value
101 'so': (1, 0b110), 'un': (1, 0b110), # same value
102 'ns': (1, 0b111), 'nu': (1, 0b111), # same value
104 assert encoding
in pmap
, \
105 "encoding %s for predicate not recognised" % encoding
106 return pmap
[encoding
]
109 # decodes "Mode" in similar way to BO field (supposed to, anyway)
110 def decode_bo(encoding
):
111 pmap
= { # TODO: double-check that these are the same as Branch BO
113 'nl': 0b001, 'ge': 0b001, # same value
115 'ng': 0b011, 'le': 0b011, # same value
118 'so': 0b110, 'un': 0b110, # same value
119 'ns': 0b111, 'nu': 0b111, # same value
121 assert encoding
in pmap
, \
122 "encoding %s for BO Mode not recognised" % encoding
123 return pmap
[encoding
]
125 # partial-decode fail-first mode
128 def decode_ffirst(encoding
):
129 if encoding
in ['RC1', '~RC1']:
131 return decode_bo(encoding
)
134 def decode_reg(field
):
135 # decode the field number. "5.v" or "3.s" or "9"
136 field
= field
.split(".")
137 regmode
= 'scalar' # default
141 elif field
[1] == 'v':
143 field
= int(field
[0]) # actual register number
144 return field
, regmode
147 def decode_imm(field
):
148 ldst_imm
= "(" in field
and field
[-1] == ')'
150 return field
[:-1].split("(")
155 def crf_extra(etype
, regmode
, field
, extras
):
156 """takes a CR Field number (CR0-CR127), splits into EXTRA2/3 and v3.0
157 the scalar/vector mode (crNN.v or crNN.s) changes both the format
158 of the EXTRA2/3 encoding as well as what range of registers is possible.
159 this function can be used for both BF/BFA and BA/BB/BT by first removing
160 the bottom 2 bits of BA/BB/BT then re-instating them after encoding.
161 see https://libre-soc.org/openpower/sv/svp64/appendix/#cr_extra
164 sv_extra
, field
= get_extra_cr_3bit(etype
, regmode
, field
)
165 # now sanity-check (and shrink afterwards)
166 if etype
== 'EXTRA2':
167 # 3-bit CR Field (BF, BFA) EXTRA2 encoding
168 if regmode
== 'scalar':
169 # range is CR0-CR15 in increments of 1
170 assert (sv_extra
>> 1) == 0, \
171 "scalar CR %s cannot fit into EXTRA2 %s" % \
172 (rname
, str(extras
[extra_idx
]))
173 # all good: encode as scalar
174 sv_extra
= sv_extra
& 0b01
176 # range is CR0-CR127 in increments of 16
177 assert sv_extra
& 0b111 == 0, \
178 "vector CR %s cannot fit into EXTRA2 %s" % \
179 (rname
, str(extras
[extra_idx
]))
180 # all good: encode as vector (bit 2 set)
181 sv_extra
= 0b10 |
(sv_extra
>> 3)
183 # 3-bit CR Field (BF, BFA) EXTRA3 encoding
184 if regmode
== 'scalar':
185 # range is CR0-CR31 in increments of 1
186 assert (sv_extra
>> 2) == 0, \
187 "scalar CR %s cannot fit into EXTRA3 %s" % \
188 (rname
, str(extras
[extra_idx
]))
189 # all good: encode as scalar
190 sv_extra
= sv_extra
& 0b11
192 # range is CR0-CR127 in increments of 8
193 assert sv_extra
& 0b11 == 0, \
194 "vector CR %s cannot fit into EXTRA3 %s" % \
195 (rname
, str(extras
[extra_idx
]))
196 # all good: encode as vector (bit 3 set)
197 sv_extra
= 0b100 |
(sv_extra
>> 2)
198 return sv_extra
, field
201 # decodes svp64 assembly listings and creates EXT001 svp64 prefixes
203 def __init__(self
, lst
, bigendian
=False, macros
=None):
208 self
.trans
= self
.translate(lst
)
209 self
.isa
= ISA() # reads the v3.0B pseudo-code markdown files
210 self
.svp64
= SVP64RM() # reads the svp64 Remap entries for registers
211 assert bigendian
== False, "error, bigendian not supported yet"
214 yield from self
.trans
216 def translate_one(self
, insn
, macros
=None):
219 macros
.update(self
.macros
)
222 # find first space, to get opcode
225 # now find opcode fields
226 fields
= ''.join(ls
[1:]).split(',')
227 mfields
= list(map(str.strip
, fields
))
228 log("opcode, fields", ls
, opcode
, mfields
)
231 for field
in mfields
:
232 fields
.append(macro_subst(macros
, field
))
233 log("opcode, fields substed", ls
, opcode
, fields
)
235 # this is a *32-bit-only* instruction. it controls SVSTATE.
236 # it is *not* a 64-bit-prefixed Vector instruction (no sv.setvl),
237 # it is a Vector *control* instruction.
238 # note: EXT022 is the "sandbox" major opcode so it's fine to add
240 # sigh have to do setvl here manually for now...
241 # note the subtract one from SVi.
242 if opcode
in ["setvl", "setvl."]:
243 # 1.6.28 SVL-FORM - from fields.txt
244 # |0 |6 |11 |16 |23 |24 |25 |26 |31 |
245 # | PO | RT | RA | SVi |ms |vs |vf | XO |Rc |
246 insn
= 22 << (31-5) # opcode 22, bits 0-5
247 fields
= list(map(int, fields
))
248 insn |
= fields
[0] << (31-10) # RT , bits 6-10
249 insn |
= fields
[1] << (31-15) # RA , bits 11-15
250 insn |
= (fields
[2]-1) << (31-22) # SVi , bits 16-22
251 insn |
= fields
[3] << (31-25) # vf , bit 25
252 insn |
= fields
[4] << (31-24) # vs , bit 24
253 insn |
= fields
[5] << (31-23) # ms , bit 23
254 insn |
= 0b11011 << (31-30) # XO , bits 26..30
255 if opcode
== 'setvl.':
256 insn |
= 1 << (31-31) # Rc=1 , bit 31
257 log("setvl", bin(insn
))
258 yield ".long 0x%x" % insn
261 # this is a 32-bit instruction. it updates SVSTATE.
262 # it *can* be SVP64-prefixed, to indicate that its registers
264 # note: EXT022 is the "sandbox" major opcode so it's fine to add
266 # sigh have to do setvl here manually for now...
267 # note the subtract one from SVi.
268 if opcode
in ["svstep", "svstep."]:
269 # 1.6.28 SVL-FORM - from fields.txt
270 # |0 |6 |11 |16 |23 |24 |25 |26 |31 |
271 # | PO | RT | RA | SVi |ms |vs |vf | XO |Rc |
272 insn
= 22 << (31-5) # opcode 22, bits 0-5
273 fields
= list(map(int, fields
))
274 insn |
= fields
[0] << (31-10) # RT , bits 6-10
275 insn |
= (fields
[1]-1) << (31-22) # SVi , bits 16-22
276 insn |
= fields
[2] << (31-25) # vf , bit 25
277 insn |
= 0b10011 << (31-30) # XO , bits 26..30
278 if opcode
== 'svstep.':
279 insn |
= 1 << (31-31) # Rc=1 , bit 31
280 log("svstep", bin(insn
))
281 yield ".long 0x%x" % insn
284 # this is a *32-bit-only* instruction. it updates SVSHAPE and SVSTATE.
285 # it is *not* a 64-bit-prefixed Vector instruction (no sv.svshape),
286 # it is a Vector *control* instruction.
287 # note: EXT022 is the "sandbox" major opcode so it's fine to add
289 # and svshape. note that the dimension fields one subtracted from each
290 if opcode
== 'svshape':
291 # 1.6.33 SVM-FORM from fields.txt
292 # |0 |6 |11 |16 |21 |25 |26 |31 |
293 # |PO | SVxd | SVyd | SVzd | SVRM |vf | XO | / |
294 insn
= 22 << (31-5) # opcode 22, bits 0-5
295 fields
= list(map(int, fields
))
296 insn |
= (fields
[0]-1) << (31-10) # SVxd , bits 6-10
297 insn |
= (fields
[1]-1) << (31-15) # SVyd , bits 11-15
298 insn |
= (fields
[2]-1) << (31-20) # SVzd , bits 16-20
299 insn |
= (fields
[3]) << (31-24) # SVRM , bits 21-24
300 insn |
= (fields
[4]) << (31-25) # vf , bits 25
301 insn |
= 0b011001 << (31-31) # XO , bits 26..31
303 log("svshape", bin(insn
))
304 yield ".long 0x%x" % insn
307 # this is a *32-bit-only* instruction. it updates the SVSHAPE SPR
308 # it is *not* a 64-bit-prefixed Vector instruction (no sv.svremap),
309 # it is a Vector *control* instruction.
310 # note: EXT022 is the "sandbox" major opcode so it's fine to add
313 if opcode
== 'svremap':
314 # 1.6.34 SVRM-FORM from fields.txt
315 # |0 |6 |11 |13 |15 |17 |19 |21 |22 |26 |31 |
316 # |PO | SVme |mi0 | mi1 | mi2 | mo0 | mo1 |pst |/// | XO | / |
317 insn
= 22 << (31-5) # opcode 22, bits 0-5
318 fields
= list(map(int, fields
))
319 insn |
= fields
[0] << (31-10) # SVme , bits 6-10
320 insn |
= fields
[1] << (31-12) # mi0 , bits 11-12
321 insn |
= fields
[2] << (31-14) # mi1 , bits 13-14
322 insn |
= fields
[3] << (31-16) # mi2 , bits 15-16
323 insn |
= fields
[4] << (31-18) # m00 , bits 17-18
324 insn |
= fields
[5] << (31-20) # m01 , bits 19-20
325 insn |
= fields
[6] << (31-21) # pst , bit 21
326 insn |
= 0b111001 << (31-31) # XO , bits 26..31
327 log("svremap", bin(insn
))
328 yield ".long 0x%x" % insn
331 # ok from here-on down these are added as 32-bit instructions
332 # and are here only because binutils (at present) doesn't have
333 # them (that's being fixed!)
334 # they can - if implementations then choose - be Vectorised
335 # (sv.fsins) because they are general-purpose scalar instructions
338 # |0 |6 |11 |16 |21 |26 |27 31|
339 # | PO | RT | RA | RB |bm |L | XO |
340 if opcode
== ('bmask'):
341 fields
= list(map(int, fields
))
342 insn
= 22 << (31-5) # opcode 22, bits 0-5
343 insn |
= fields
[0] << (31-10) # RT , bits 6-10
344 insn |
= fields
[1] << (31-15) # RA , bits 11-15
345 insn |
= fields
[2] << (31-20) # RB , bits 16-20
346 insn |
= fields
[3] << (31-25) # mask , bits 21-25
347 insn |
= fields
[4] << (31-26) # L , bit 26
348 insn |
= 0b010001 << (31-31) # XO , bits 26..31
349 log("bmask", bin(insn
))
350 yield ".long 0x%x" % insn
355 # XXX WARNING THESE ARE NOT APPROVED BY OPF ISA WG
356 # however we are out of space with opcode 22
357 if opcode
.startswith('fsins'):
358 fields
= list(map(int, fields
))
359 insn
= 59 << (31-5) # opcode 59, bits 0-5
360 insn |
= fields
[0] << (31-10) # RT , bits 6-10
361 insn |
= fields
[1] << (31-20) # RB , bits 16-20
362 insn |
= 0b1000001110 << (31-30) # XO , bits 21..30
363 if opcode
== 'fsins.':
364 insn |
= 1 << (31-31) # Rc=1 , bit 31
365 log("fsins", bin(insn
))
366 yield ".long 0x%x" % insn
370 # XXX WARNING THESE ARE NOT APPROVED BY OPF ISA WG
371 # however we are out of space with opcode 22
372 if opcode
.startswith('fcoss'):
373 fields
= list(map(int, fields
))
374 insn
= 59 << (31-5) # opcode 59, bits 0-5
375 insn |
= fields
[0] << (31-10) # RT , bits 6-10
376 insn |
= fields
[1] << (31-20) # RB , bits 16-20
377 insn |
= 0b1000101110 << (31-30) # XO , bits 21..30
378 if opcode
== 'fcoss.':
379 insn |
= 1 << (31-31) # Rc=1 , bit 31
380 log("fcoss", bin(insn
))
381 yield ".long 0x%x" % insn
384 # XXX WARNING THESE ARE NOT APPROVED BY OPF ISA WG
385 # however we are out of space with opcode 22
386 if opcode
in ('ternlogi', 'ternlogi.'):
395 instr
= (instr
<< 5) | rt
396 instr
= (instr
<< 5) | ra
397 instr
= (instr
<< 5) | rb
398 instr
= (instr
<< 8) | imm
399 instr
= (instr
<< 2) | xo
400 instr
= (instr
<< 1) | rc
401 asm
= f
"{opcode} {rt}, {ra}, {rb}, {imm}"
402 yield f
".4byte {hex(instr)} # {asm}"
405 # XXX WARNING THESE ARE NOT APPROVED BY OPF ISA WG
406 # however we are out of space with opcode 22
407 if opcode
in ('grev', 'grevi', 'grevw', 'grevwi',
408 'grev.', 'grevi.', 'grevw.', 'grevwi.'):
410 # _ matches fields in table at:
411 # https://libre-soc.org/openpower/sv/bitmanip/
417 Rc
= 1 if '.' in opcode
else 0
420 rb_imm
= int(fields
[2])
422 instr
= (instr
<< 5) | rt
423 instr
= (instr
<< 5) | ra
424 if opcode
== 'grevi' or opcode
== 'grevi.':
425 assert 0 <= rb_imm
< 64
426 instr
= (instr
<< 6) | rb_imm
427 instr
= (instr
<< 9) | xo
429 assert 0 <= rb_imm
< 32
430 instr
= (instr
<< 5) | rb_imm
431 instr
= (instr
<< 10) | xo
432 instr
= (instr
<< 1) | Rc
433 asm
= f
"{opcode} {rt}, {ra}, {rb_imm}"
434 yield f
".4byte {hex(instr)} # {asm}"
438 # XXX WARNING THESE ARE NOT APPROVED BY OPF ISA WG
440 # |0 |6 |7|8|9 |10 |11|12|13 |15|16|17 |20|21 |31 |
441 # | PO | RT | RA | RB | XO |Rc |
442 if opcode
in ['mins', 'maxs', 'minu', 'maxu',
443 'mins.', 'maxs.', 'minu.', 'maxu.']:
444 if opcode
[:4] == 'maxs':
446 if opcode
[:4] == 'maxu':
448 if opcode
[:4] == 'mins':
450 if opcode
[:4] == 'minu':
452 fields
= list(map(int, fields
))
453 insn
= 22 << (31-5) # opcode 22, bits 0-5
454 insn |
= fields
[0] << (31-10) # RT , bits 6-10
455 insn |
= fields
[1] << (31-15) # RA , bits 11-15
456 insn |
= fields
[2] << (31-20) # RB , bits 16-20
457 insn |
= XO
<< (31-30) # XO , bits 21..30
458 if opcode
.endswith('.'):
459 insn |
= 1 << (31-31) # Rc=1 , bit 31
460 log("maxs", bin(insn
))
461 yield ".long 0x%x" % insn
464 # and avgadd, absdu, absdacu, absdacs
465 # XXX WARNING THESE ARE NOT APPROVED BY OPF ISA WG
467 # |0 |6 |7|8|9 |10 |11|12|13 |15|16|17 |20|21 |31 |
468 # | PO | RT | RA | RB | XO |Rc |
469 if opcode
in ['avgadd', 'absdu', 'absds', 'absdacu', 'absdacs',
471 if opcode
[:5] == 'absdu':
473 elif opcode
[:5] == 'absds':
475 elif opcode
[:6] == 'avgadd':
477 elif opcode
[:7] == 'absdacu':
479 elif opcode
[:7] == 'absdacs':
481 elif opcode
[:7] == 'cprop':
483 fields
= list(map(int, fields
))
484 insn
= 22 << (31-5) # opcode 22, bits 0-5
485 insn |
= fields
[0] << (31-10) # RT , bits 6-10
486 insn |
= fields
[1] << (31-15) # RA , bits 11-15
487 insn |
= fields
[2] << (31-20) # RB , bits 16-20
488 insn |
= XO
<< (31-30) # XO , bits 21..30
489 if opcode
.endswith('.'):
490 insn |
= 1 << (31-31) # Rc=1 , bit 31
491 log(opcode
, bin(insn
))
492 yield ".long 0x%x" % insn
495 # identify if is a svp64 mnemonic
496 if not opcode
.startswith('sv.'):
497 yield insn
# unaltered
499 opcode
= opcode
[3:] # strip leading "sv"
501 # start working on decoding the svp64 op: sv.basev30Bop/vec2/mode
502 opmodes
= opcode
.split("/") # split at "/"
503 v30b_op
= opmodes
.pop(0) # first is the v3.0B
504 # check instruction ends with dot
505 rc_mode
= v30b_op
.endswith('.')
507 v30b_op
= v30b_op
[:-1]
509 # sigh again, have to recognised LD/ST bit-reverse instructions
510 # this has to be "processed" to fit into a v3.0B without the "sh"
511 # e.g. ldsh is actually ld
512 ldst_shift
= v30b_op
.startswith("l") and v30b_op
.endswith("sh")
514 if v30b_op
not in isa
.instr
:
515 raise Exception("opcode %s of '%s' not supported" %
519 # okaay we need to process the fields and make this:
520 # ldsh RT, SVD(RA), RC - 11 bits for SVD, 5 for RC
522 # ld RT, D(RA) - 16 bits
523 # likewise same for SVDS (9 bits for SVDS, 5 for RC, 14 bits for DS)
524 form
= isa
.instr
[v30b_op
].form
# get form (SVD-Form, SVDS-Form)
528 # identify if this is a ld/st immediate(reg) thing
529 ldst_imm
= "(" in field
and field
[-1] == ')'
531 newfields
.append(field
[:-1].split("("))
533 newfields
.append(field
)
535 immed
, RA
= newfields
[1]
537 RC
= int(newfields
.pop(2)) # better be an integer number!
538 if form
== 'SVD': # 16 bit: immed 11 bits, RC shift up 11
539 immed
= (immed
& 0b11111111111) |
(RC
<< 11)
540 if immed
& (1 << 15): # should be negative
542 if form
== 'SVDS': # 14 bit: immed 9 bits, RC shift up 9
543 immed
= (immed
& 0b111111111) |
(RC
<< 9)
544 if immed
& (1 << 13): # should be negative
546 newfields
[1] = "%d(%s)" % (immed
, RA
)
549 # and strip off "sh" from end, and add "sh" to opmodes, instead
550 v30b_op
= v30b_op
[:-2]
552 log("rewritten", v30b_op
, opmodes
, fields
)
554 if v30b_op
not in svp64
.instrs
:
555 raise Exception("opcode %s of '%s' not an svp64 instruction" %
557 v30b_regs
= isa
.instr
[v30b_op
].regs
[0] # get regs info "RT, RA, RB"
558 rm
= svp64
.instrs
[v30b_op
] # one row of the svp64 RM CSV
559 log("v3.0B op", v30b_op
, "Rc=1" if rc_mode
else '')
560 log("v3.0B regs", opcode
, v30b_regs
)
563 # right. the first thing to do is identify the ordering of
564 # the registers, by name. the EXTRA2/3 ordering is in
565 # rm['0']..rm['3'] but those fields contain the names RA, BB
566 # etc. we have to read the pseudocode to understand which
567 # reg is which in our instruction. sigh.
569 # first turn the svp64 rm into a "by name" dict, recording
570 # which position in the RM EXTRA it goes into
571 # also: record if the src or dest was a CR, for sanity-checking
572 # (elwidth overrides on CRs are banned)
573 decode
= decode_extra(rm
)
574 dest_reg_cr
, src_reg_cr
, svp64_src
, svp64_dest
= decode
576 log("EXTRA field index, src", svp64_src
)
577 log("EXTRA field index, dest", svp64_dest
)
579 # okaaay now we identify the field value (opcode N,N,N) with
580 # the pseudo-code info (opcode RT, RA, RB)
581 assert len(fields
) == len(v30b_regs
), \
582 "length of fields %s must match insn `%s` fields %s" % \
583 (str(v30b_regs
), insn
, str(fields
))
584 opregfields
= zip(fields
, v30b_regs
) # err that was easy
586 # now for each of those find its place in the EXTRA encoding
587 # note there is the possibility (for LD/ST-with-update) of
588 # RA occurring **TWICE**. to avoid it getting added to the
589 # v3.0B suffix twice, we spot it as a duplicate, here
590 extras
= OrderedDict()
591 for idx
, (field
, regname
) in enumerate(opregfields
):
592 imm
, regname
= decode_imm(regname
)
593 rtype
= get_regtype(regname
)
594 log(" idx find", rtype
, idx
, field
, regname
, imm
)
596 # probably an immediate field, append it straight
597 extras
[('imm', idx
, False)] = (idx
, field
, None, None, None)
599 extra
= svp64_src
.get(regname
, None)
600 if extra
is not None:
601 extra
= ('s', extra
, False) # not a duplicate
602 extras
[extra
] = (idx
, field
, regname
, rtype
, imm
)
603 log(" idx src", idx
, extra
, extras
[extra
])
604 dextra
= svp64_dest
.get(regname
, None)
605 log("regname in", regname
, dextra
)
606 if dextra
is not None:
607 is_a_duplicate
= extra
is not None # duplicate spotted
608 dextra
= ('d', dextra
, is_a_duplicate
)
609 extras
[dextra
] = (idx
, field
, regname
, rtype
, imm
)
610 log(" idx dst", idx
, extra
, extras
[dextra
])
612 # great! got the extra fields in their associated positions:
613 # also we know the register type. now to create the EXTRA encodings
614 etype
= rm
['Etype'] # Extra type: EXTRA3/EXTRA2
615 ptype
= rm
['Ptype'] # Predication type: Twin / Single
618 for extra_idx
, (idx
, field
, rname
, rtype
, iname
) in extras
.items():
619 # is it a field we don't alter/examine? if so just put it
622 v30b_newfields
.append(field
)
625 # identify if this is a ld/st immediate(reg) thing
626 ldst_imm
= "(" in field
and field
[-1] == ')'
628 immed
, field
= field
[:-1].split("(")
630 field
, regmode
= decode_reg(field
)
631 log(" ", extra_idx
, rname
, rtype
,
632 regmode
, iname
, field
, end
=" ")
634 # see Mode field https://libre-soc.org/openpower/sv/svp64/
635 # XXX TODO: the following is a bit of a laborious repeated
636 # mess, which could (and should) easily be parameterised.
637 # XXX also TODO: the LD/ST modes which are different
638 # https://libre-soc.org/openpower/sv/ldst/
640 # rright. SVP64 register numbering is from 0 to 127
641 # for GPRs, FPRs *and* CR Fields, where for v3.0 the GPRs and RPFs
642 # are 0-31 and CR Fields are only 0-7. the SVP64 RM "Extra"
643 # area is used to extend the numbering from the 32-bit
644 # instruction, and also to record whether the register
645 # is scalar or vector. on a per-operand basis. this
646 # results in a slightly finnicky encoding: here we go...
648 # encode SV-GPR and SV-FPR field into extra, v3.0field
649 if rtype
in ['GPR', 'FPR']:
650 sv_extra
, field
= get_extra_gpr(etype
, regmode
, field
)
651 # now sanity-check. EXTRA3 is ok, EXTRA2 has limits
652 # (and shrink to a single bit if ok)
653 if etype
== 'EXTRA2':
654 if regmode
== 'scalar':
655 # range is r0-r63 in increments of 1
656 assert (sv_extra
>> 1) == 0, \
657 "scalar GPR %s cannot fit into EXTRA2 %s" % \
658 (rname
, str(extras
[extra_idx
]))
659 # all good: encode as scalar
660 sv_extra
= sv_extra
& 0b01
662 # range is r0-r127 in increments of 2 (r0 r2 ... r126)
663 assert sv_extra
& 0b01 == 0, \
664 "%s: vector field %s cannot fit " \
666 (insn
, rname
, str(extras
[extra_idx
]))
667 # all good: encode as vector (bit 2 set)
668 sv_extra
= 0b10 |
(sv_extra
>> 1)
669 elif regmode
== 'vector':
670 # EXTRA3 vector bit needs marking
673 # encode SV-CR 3-bit field into extra, v3.0field.
674 # 3-bit is for things like BF and BFA
675 elif rtype
== 'CR_3bit':
676 sv_extra
, field
= crf_extra(etype
, regmode
, field
, extras
)
678 # encode SV-CR 5-bit field into extra, v3.0field
679 # 5-bit is for things like BA BB BC BT etc.
680 # *sigh* this is the same as 3-bit except the 2 LSBs of the
681 # 5-bit field are passed through unaltered.
682 elif rtype
== 'CR_5bit':
683 cr_subfield
= field
& 0b11 # record bottom 2 bits for later
684 field
= field
>> 2 # strip bottom 2 bits
685 # use the exact same 3-bit function for the top 3 bits
686 sv_extra
, field
= crf_extra(etype
, regmode
, field
, extras
)
687 # reconstruct the actual 5-bit CR field (preserving the
688 # bottom 2 bits, unaltered)
689 field
= (field
<< 2) | cr_subfield
692 raise Exception("no type match: %s" % rtype
)
694 # capture the extra field info
695 log("=>", "%5s" % bin(sv_extra
), field
)
696 extras
[extra_idx
] = sv_extra
698 # append altered field value to v3.0b, differs for LDST
699 # note that duplicates are skipped e.g. EXTRA2 contains
700 # *BOTH* s:RA *AND* d:RA which happens on LD/ST-with-update
701 srcdest
, idx
, duplicate
= extra_idx
702 if duplicate
: # skip adding to v3.0b fields, already added
705 v30b_newfields
.append(("%s(%s)" % (immed
, str(field
))))
707 v30b_newfields
.append(str(field
))
709 log("new v3.0B fields", v30b_op
, v30b_newfields
)
710 log("extras", extras
)
712 # rright. now we have all the info. start creating SVP64 RM
713 svp64_rm
= SVP64RMFields()
715 # begin with EXTRA fields
716 for idx
, sv_extra
in extras
.items():
722 srcdest
, idx
, duplicate
= idx
723 if etype
== 'EXTRA2':
724 svp64_rm
.extra2
[idx
].eq(
725 SelectableInt(sv_extra
, SVP64RM_EXTRA2_SPEC_SIZE
))
727 svp64_rm
.extra3
[idx
].eq(
728 SelectableInt(sv_extra
, SVP64RM_EXTRA3_SPEC_SIZE
))
730 # identify if the op is a LD/ST. the "blegh" way. copied
731 # from power_enums. TODO, split the list _insns down.
733 "lbarx", "lbz", "lbzu", "lbzux", "lbzx", # load byte
734 "ld", "ldarx", "ldbrx", "ldu", "ldux", "ldx", # load double
735 "lfs", "lfsx", "lfsu", "lfsux", # FP load single
736 "lfd", "lfdx", "lfdu", "lfdux", "lfiwzx", "lfiwax", # FP load dbl
737 "lha", "lharx", "lhau", "lhaux", "lhax", # load half
738 "lhbrx", "lhz", "lhzu", "lhzux", "lhzx", # more load half
739 "lwa", "lwarx", "lwaux", "lwax", "lwbrx", # load word
740 "lwz", "lwzcix", "lwzu", "lwzux", "lwzx", # more load word
743 "stb", "stbcix", "stbcx", "stbu", "stbux", "stbx",
744 "std", "stdbrx", "stdcx", "stdu", "stdux", "stdx",
745 "stfs", "stfsx", "stfsu", "stfux", # FP store sgl
746 "stfd", "stfdx", "stfdu", "stfdux", "stfiwx", # FP store dbl
747 "sth", "sthbrx", "sthcx", "sthu", "sthux", "sthx",
748 "stw", "stwbrx", "stwcx", "stwu", "stwux", "stwx",
750 # use this to determine if the SVP64 RM format is different.
751 # see https://libre-soc.org/openpower/sv/ldst/
752 is_ldst
= is_ld
or is_st
754 # branch-conditional detection
762 destwid
= 0 # bits 4-5
763 srcwid
= 0 # bits 6-7
765 smask
= 0 # bits 16-18 but only for twin-predication
766 mode
= 0 # bits 19-23
768 mask_m_specified
= False
779 mapreduce_crm
= False
780 mapreduce_svm
= False
786 # branch-conditional bits
796 # ok let's start identifying opcode augmentation fields
797 for encmode
in opmodes
:
798 # predicate mask (src and dest)
799 if encmode
.startswith("m="):
801 pmmode
, pmask
= decode_predicate(encmode
[2:])
802 smmode
, smask
= pmmode
, pmask
804 mask_m_specified
= True
805 # predicate mask (dest)
806 elif encmode
.startswith("dm="):
808 pmmode
, pmask
= decode_predicate(encmode
[3:])
811 # predicate mask (src, twin-pred)
812 elif encmode
.startswith("sm="):
814 smmode
, smask
= decode_predicate(encmode
[3:])
818 elif encmode
.startswith("sh"):
821 elif encmode
.startswith("vec"):
822 subvl
= decode_subvl(encmode
[3:])
824 elif encmode
.startswith("ew="):
825 destwid
= decode_elwidth(encmode
[3:])
826 elif encmode
.startswith("sw="):
827 srcwid
= decode_elwidth(encmode
[3:])
828 # element-strided LD/ST
829 elif encmode
== 'els':
832 elif encmode
== 'sats':
833 assert sv_mode
is None
836 elif encmode
== 'satu':
837 assert sv_mode
is None
841 elif encmode
== 'sz':
843 elif encmode
== 'dz':
846 elif encmode
.startswith("ff="):
847 assert sv_mode
is None
849 failfirst
= decode_ffirst(encmode
[3:])
850 # predicate-result, interestingly same as fail-first
851 elif encmode
.startswith("pr="):
852 assert sv_mode
is None
854 predresult
= decode_ffirst(encmode
[3:])
855 # map-reduce mode, reverse-gear
856 elif encmode
== 'mrr':
857 assert sv_mode
is None
862 elif encmode
== 'mr':
863 assert sv_mode
is None
866 elif encmode
== 'crm': # CR on map-reduce
867 assert sv_mode
is None
870 elif encmode
== 'svm': # sub-vector mode
875 elif encmode
== 'st': # svstep mode
877 elif encmode
== 'sr': # svstep BRc mode
880 elif encmode
== 'vs': # VLSET mode
882 elif encmode
== 'vsi': # VLSET mode with VLI (VL inclusives)
885 elif encmode
== 'vsb': # VLSET mode with VSb
888 elif encmode
== 'vsbi': # VLSET mode with VLI and VSb
892 elif encmode
== 'snz': # sz (only) already set above
895 elif encmode
== 'lu': # LR update mode
898 raise AssertionError("unknown encmode %s" % encmode
)
900 raise AssertionError("unknown encmode %s" % encmode
)
903 # since m=xx takes precedence (overrides) sm=xx and dm=xx,
904 # treat them as mutually exclusive
906 assert not has_smask
,\
907 "cannot have both source-mask and predicate mask"
908 assert not has_pmask
,\
909 "cannot have both dest-mask and predicate mask"
910 # since the default is INT predication (ALWAYS), if you
911 # specify one CR mask, you must specify both, to avoid
912 # mixing INT and CR reg types
913 if has_pmask
and pmmode
== 1:
915 "need explicit source-mask in CR twin predication"
916 if has_smask
and smmode
== 1:
918 "need explicit dest-mask in CR twin predication"
919 # sanity-check that 2Pred mask is same mode
920 if has_pmask
and has_smask
:
921 assert smmode
== pmmode
, \
922 "predicate masks %s and %s must be same reg type" % \
925 # sanity-check that twin-predication mask only specified in 2P mode
927 assert not has_smask
, \
928 "source-mask can only be specified on Twin-predicate ops"
929 assert not has_pmask
, \
930 "dest-mask can only be specified on Twin-predicate ops"
932 # construct the mode field, doing sanity-checking along the way
934 assert sv_mode
== 0b00, "sub-vector mode in mapreduce only"
935 assert subvl
!= 0, "sub-vector mode not possible on SUBVL=1"
938 assert has_smask
or mask_m_specified
, \
939 "src zeroing requires a source predicate"
941 assert has_pmask
or mask_m_specified
, \
942 "dest zeroing requires a dest predicate"
944 # check LDST shifted, only available in "normal" mode
945 if is_ldst
and ldst_shift
:
946 assert sv_mode
is None, \
947 "LD shift cannot have modes (%s) applied" % sv_mode
949 # okaaay, so there are 4 different modes, here, which will be
950 # partly-merged-in: is_ldst is merged in with "normal", but
951 # is_bc is so different it's done separately. likewise is_cr
952 # (when it is done). here are the maps:
954 # for "normal" arithmetic: https://libre-soc.org/openpower/sv/normal/
956 | 0-1 | 2 | 3 4 | description |
957 | --- | --- |---------|-------------------------- |
958 | 00 | 0 | dz sz | normal mode |
959 | 00 | 1 | 0 RG | scalar reduce mode (mapreduce), SUBVL=1 |
960 | 00 | 1 | 1 / | parallel reduce mode (mapreduce), SUBVL=1 |
961 | 00 | 1 | SVM RG | subvector reduce mode, SUBVL>1 |
962 | 01 | inv | CR-bit | Rc=1: ffirst CR sel |
963 | 01 | inv | VLi RC1 | Rc=0: ffirst z/nonz |
964 | 10 | N | dz sz | sat mode: N=0/1 u/s |
965 | 11 | inv | CR-bit | Rc=1: pred-result CR sel |
966 | 11 | inv | dz RC1 | Rc=0: pred-result z/nonz |
969 # https://libre-soc.org/openpower/sv/ldst/
970 # for LD/ST-immediate:
972 | 0-1 | 2 | 3 4 | description |
973 | --- | --- |---------|--------------------------- |
974 | 00 | 0 | dz els | normal mode |
975 | 00 | 1 | dz shf | shift mode |
976 | 01 | inv | CR-bit | Rc=1: ffirst CR sel |
977 | 01 | inv | els RC1 | Rc=0: ffirst z/nonz |
978 | 10 | N | dz els | sat mode: N=0/1 u/s |
979 | 11 | inv | CR-bit | Rc=1: pred-result CR sel |
980 | 11 | inv | els RC1 | Rc=0: pred-result z/nonz |
983 # for LD/ST-indexed (RA+RB):
985 | 0-1 | 2 | 3 4 | description |
986 | --- | --- |---------|-------------------------- |
987 | 00 | SEA | dz sz | normal mode |
988 | 01 | SEA | dz sz | Strided (scalar only source) |
989 | 10 | N | dz sz | sat mode: N=0/1 u/s |
990 | 11 | inv | CR-bit | Rc=1: pred-result CR sel |
991 | 11 | inv | dz RC1 | Rc=0: pred-result z/nonz |
994 # and leaving out branches and cr_ops for now because they're
996 """ TODO branches and cr_ops
999 # now create mode and (overridden) src/dst widths
1000 # XXX TODO: sanity-check bc modes
1002 sv_mode
= ((bc_svstep
<< SVP64MODE
.MOD2_MSB
) |
1003 (bc_vlset
<< SVP64MODE
.MOD2_LSB
) |
1004 (bc_snz
<< SVP64MODE
.BC_SNZ
))
1005 srcwid
= (bc_vsb
<< 1) | bc_lru
1006 destwid
= (bc_lru
<< 1) | bc_all
1010 ######################################
1013 mode |
= src_zero
<< SVP64MODE
.SZ
# predicate zeroing
1014 mode |
= dst_zero
<< SVP64MODE
.DZ
# predicate zeroing
1016 # TODO: for now, LD/ST-indexed is ignored.
1017 mode |
= ldst_elstride
<< SVP64MODE
.ELS_NORMAL
# el-strided
1020 mode |
= 1 << SVP64MODE
.LDST_SHIFT
1022 # TODO, reduce and subvector mode
1023 # 00 1 dz CRM reduce mode (mapreduce), SUBVL=1
1024 # 00 1 SVM CRM subvector reduce mode, SUBVL>1
1028 ######################################
1030 elif sv_mode
== 0b00:
1031 mode |
= (0b1 << SVP64MODE
.REDUCE
) # sets mapreduce
1032 assert dst_zero
== 0, "dest-zero not allowed in mapreduce mode"
1034 mode |
= (0b1 << SVP64MODE
.RG
) # sets Reverse-gear mode
1036 mode |
= (0b1 << SVP64MODE
.CRM
) # sets CRM mode
1037 assert rc_mode
, "CRM only allowed when Rc=1"
1038 # bit of weird encoding to jam zero-pred or SVM mode in.
1039 # SVM mode can be enabled only when SUBVL=2/3/4 (vec2/3/4)
1041 mode |
= dst_zero
<< SVP64MODE
.DZ
# predicate zeroing
1043 mode |
= (0b1 << SVP64MODE
.SVM
) # sets SVM mode
1045 ######################################
1047 elif sv_mode
== 0b01:
1048 assert src_zero
== 0, "dest-zero not allowed in failfirst mode"
1049 if failfirst
== 'RC1':
1050 mode |
= (0b1 << SVP64MODE
.RC1
) # sets RC1 mode
1051 mode |
= (dst_zero
<< SVP64MODE
.DZ
) # predicate dst-zeroing
1052 assert rc_mode
== False, "ffirst RC1 only ok when Rc=0"
1053 elif failfirst
== '~RC1':
1054 mode |
= (0b1 << SVP64MODE
.RC1
) # sets RC1 mode
1055 mode |
= (dst_zero
<< SVP64MODE
.DZ
) # predicate dst-zeroing
1056 mode |
= (0b1 << SVP64MODE
.INV
) # ... with inversion
1057 assert rc_mode
== False, "ffirst RC1 only ok when Rc=0"
1059 assert dst_zero
== 0, "dst-zero not allowed in ffirst BO"
1060 assert rc_mode
, "ffirst BO only possible when Rc=1"
1061 mode |
= (failfirst
<< SVP64MODE
.BO_LSB
) # set BO
1063 ######################################
1064 # "saturation" modes
1065 elif sv_mode
== 0b10:
1066 mode |
= src_zero
<< SVP64MODE
.SZ
# predicate zeroing
1067 mode |
= dst_zero
<< SVP64MODE
.DZ
# predicate zeroing
1068 mode |
= (saturation
<< SVP64MODE
.N
) # signed/us saturation
1070 ######################################
1071 # "predicate-result" modes. err... code-duplication from ffirst
1072 elif sv_mode
== 0b11:
1073 assert src_zero
== 0, "dest-zero not allowed in predresult mode"
1074 if predresult
== 'RC1':
1075 mode |
= (0b1 << SVP64MODE
.RC1
) # sets RC1 mode
1076 mode |
= (dst_zero
<< SVP64MODE
.DZ
) # predicate dst-zeroing
1077 assert rc_mode
== False, "pr-mode RC1 only ok when Rc=0"
1078 elif predresult
== '~RC1':
1079 mode |
= (0b1 << SVP64MODE
.RC1
) # sets RC1 mode
1080 mode |
= (dst_zero
<< SVP64MODE
.DZ
) # predicate dst-zeroing
1081 mode |
= (0b1 << SVP64MODE
.INV
) # ... with inversion
1082 assert rc_mode
== False, "pr-mode RC1 only ok when Rc=0"
1084 assert dst_zero
== 0, "dst-zero not allowed in pr-mode BO"
1085 assert rc_mode
, "pr-mode BO only possible when Rc=1"
1086 mode |
= (predresult
<< SVP64MODE
.BO_LSB
) # set BO
1088 # whewww.... modes all done :)
1089 # now put into svp64_rm
1092 svp64_rm
.mode
.eq(SelectableInt(mode
, SVP64RM_MODE_SIZE
))
1094 # put in predicate masks into svp64_rm
1096 # source pred: bits 16-18
1097 svp64_rm
.smask
.eq(SelectableInt(smask
, SVP64RM_SMASK_SIZE
))
1099 svp64_rm
.mmode
.eq(SelectableInt(mmode
, SVP64RM_MMODE_SIZE
))
1101 svp64_rm
.mask
.eq(SelectableInt(pmask
, SVP64RM_MASK_SIZE
))
1103 # and subvl: bits 8-9
1104 svp64_rm
.subvl
.eq(SelectableInt(subvl
, SVP64RM_SUBVL_SIZE
))
1108 svp64_rm
.ewsrc
.eq(SelectableInt(srcwid
, SVP64RM_EWSRC_SIZE
))
1110 svp64_rm
.elwidth
.eq(SelectableInt(destwid
, SVP64RM_ELWIDTH_SIZE
))
1112 # nice debug printout. (and now for something completely different)
1113 # https://youtu.be/u0WOIwlXE9g?t=146
1114 svp64_rm_value
= svp64_rm
.spr
.value
1115 log("svp64_rm", hex(svp64_rm_value
), bin(svp64_rm_value
))
1116 log(" mmode 0 :", bin(mmode
))
1117 log(" pmask 1-3 :", bin(pmask
))
1118 log(" dstwid 4-5 :", bin(destwid
))
1119 log(" srcwid 6-7 :", bin(srcwid
))
1120 log(" subvl 8-9 :", bin(subvl
))
1121 log(" mode 19-23:", bin(mode
))
1122 offs
= 2 if etype
== 'EXTRA2' else 3 # 2 or 3 bits
1123 for idx
, sv_extra
in extras
.items():
1128 srcdest
, idx
, duplicate
= idx
1129 start
= (10+idx
*offs
)
1130 end
= start
+ offs
-1
1131 log(" extra%d %2d-%2d:" % (idx
, start
, end
),
1134 log(" smask 16-17:", bin(smask
))
1137 # first, construct the prefix from its subfields
1138 svp64_prefix
= SVP64PrefixFields()
1139 svp64_prefix
.major
.eq(SelectableInt(0x1, SV64P_MAJOR_SIZE
))
1140 svp64_prefix
.pid
.eq(SelectableInt(0b11, SV64P_PID_SIZE
))
1141 svp64_prefix
.rm
.eq(svp64_rm
.spr
)
1143 # fiinally yield the svp64 prefix and the thingy. v3.0b opcode
1144 rc
= '.' if rc_mode
else ''
1145 yield ".long 0x%08x" % svp64_prefix
.insn
.value
1147 # argh, sv.fmadds etc. need to be done manually
1148 if v30b_op
== 'ffmadds':
1149 opcode
= 59 << (32-6) # bits 0..6 (MSB0)
1150 opcode |
= int(v30b_newfields
[0]) << (32-11) # FRT
1151 opcode |
= int(v30b_newfields
[1]) << (32-16) # FRA
1152 opcode |
= int(v30b_newfields
[2]) << (32-21) # FRB
1153 opcode |
= int(v30b_newfields
[3]) << (32-26) # FRC
1154 opcode |
= 0b00101 << (32-31) # bits 26-30
1156 opcode |
= 1 # Rc, bit 31.
1157 yield ".long 0x%x" % opcode
1158 # argh, sv.fdmadds need to be done manually
1159 elif v30b_op
== 'fdmadds':
1160 opcode
= 59 << (32-6) # bits 0..6 (MSB0)
1161 opcode |
= int(v30b_newfields
[0]) << (32-11) # FRT
1162 opcode |
= int(v30b_newfields
[1]) << (32-16) # FRA
1163 opcode |
= int(v30b_newfields
[2]) << (32-21) # FRB
1164 opcode |
= int(v30b_newfields
[3]) << (32-26) # FRC
1165 opcode |
= 0b01111 << (32-31) # bits 26-30
1167 opcode |
= 1 # Rc, bit 31.
1168 yield ".long 0x%x" % opcode
1169 # argh, sv.ffadds etc. need to be done manually
1170 elif v30b_op
== 'ffadds':
1171 opcode
= 59 << (32-6) # bits 0..6 (MSB0)
1172 opcode |
= int(v30b_newfields
[0]) << (32-11) # FRT
1173 opcode |
= int(v30b_newfields
[1]) << (32-16) # FRA
1174 opcode |
= int(v30b_newfields
[2]) << (32-21) # FRB
1175 opcode |
= 0b01101 << (32-31) # bits 26-30
1177 opcode |
= 1 # Rc, bit 31.
1178 yield ".long 0x%x" % opcode
1179 # sigh have to do svstep here manually for now...
1180 elif opcode
in ["svstep", "svstep."]:
1181 insn
= 22 << (31-5) # opcode 22, bits 0-5
1182 insn |
= int(v30b_newfields
[0]) << (31-10) # RT , bits 6-10
1183 insn |
= int(v30b_newfields
[1]) << (31-22) # SVi , bits 16-22
1184 insn |
= int(v30b_newfields
[2]) << (31-25) # vf , bit 25
1185 insn |
= 0b10011 << (31-30) # XO , bits 26..30
1186 if opcode
== 'svstep.':
1187 insn |
= 1 << (31-31) # Rc=1 , bit 31
1188 log("svstep", bin(insn
))
1189 yield ".long 0x%x" % insn
1190 # argh, sv.fcoss etc. need to be done manually
1191 elif v30b_op
in ["fcoss", "fcoss."]:
1192 insn
= 59 << (31-5) # opcode 59, bits 0-5
1193 insn |
= int(v30b_newfields
[0]) << (31-10) # RT , bits 6-10
1194 insn |
= int(v30b_newfields
[1]) << (31-20) # RB , bits 16-20
1195 insn |
= 0b1000101110 << (31-30) # XO , bits 21..30
1196 if opcode
== 'fcoss.':
1197 insn |
= 1 << (31-31) # Rc=1 , bit 31
1198 log("fcoss", bin(insn
))
1199 yield ".long 0x%x" % insn
1202 yield "%s %s" % (v30b_op
+rc
, ", ".join(v30b_newfields
))
1203 log("new v3.0B fields", v30b_op
, v30b_newfields
)
1205 def translate(self
, lst
):
1207 yield from self
.translate_one(insn
)
1210 def macro_subst(macros
, txt
):
1212 log("subst", txt
, macros
)
1215 for macro
, value
in macros
.items():
1218 replaced
= txt
.replace(macro
, value
)
1219 log("macro", txt
, "replaced", replaced
, macro
, value
)
1222 toreplace
= '%s.s' % macro
1223 if toreplace
== txt
:
1225 replaced
= txt
.replace(toreplace
, "%s.s" % value
)
1226 log("macro", txt
, "replaced", replaced
, toreplace
, value
)
1229 toreplace
= '%s.v' % macro
1230 if toreplace
== txt
:
1232 replaced
= txt
.replace(toreplace
, "%s.v" % value
)
1233 log("macro", txt
, "replaced", replaced
, toreplace
, value
)
1236 toreplace
= '(%s)' % macro
1237 if toreplace
in txt
:
1239 replaced
= txt
.replace(toreplace
, '(%s)' % value
)
1240 log("macro", txt
, "replaced", replaced
, toreplace
, value
)
1243 log(" processed", txt
)
1251 if not line
[0].isspace():
1259 # get an input file and an output file
1263 outfile
= sys
.stdout
1264 # read the whole lot in advance in case of in-place
1265 lines
= list(infile
.readlines())
1266 elif len(args
) != 2:
1267 print("pysvp64asm [infile | -] [outfile | -]", file=sys
.stderr
)
1273 infile
= open(args
[0], "r")
1274 # read the whole lot in advance in case of in-place overwrite
1275 lines
= list(infile
.readlines())
1278 outfile
= sys
.stdout
1280 outfile
= open(args
[1], "w")
1282 # read the line, look for "sv", process it
1283 macros
= {} # macros which start ".set"
1286 op
= line
.split("#")[0].strip()
1288 if op
.startswith(".set"):
1289 macro
= op
[4:].split(",")
1290 (macro
, value
) = map(str.strip
, macro
)
1291 macros
[macro
] = value
1292 if not (op
.startswith("sv.") or
1293 op
.startswith("setvl") or
1294 op
.startswith("svshape")):
1298 (ws
, line
) = get_ws(line
)
1299 lst
= isa
.translate_one(op
, macros
)
1300 lst
= '; '.join(lst
)
1301 outfile
.write("%s%s # %s\n" % (ws
, lst
, op
))
1304 if __name__
== '__main__':
1305 lst
= ['slw 3, 1, 4',
1308 'sv.cmpi 5, 1, 3, 2',
1310 'sv.isel 64.v, 3, 2, 65.v',
1311 'sv.setb/dm=r3/sm=1<<r3 5, 31',
1312 'sv.setb/m=r3 5, 31',
1313 'sv.setb/vec2 5, 31',
1314 'sv.setb/sw=8/ew=16 5, 31',
1315 'sv.extsw./ff=eq 5, 31',
1316 'sv.extsw./satu/sz/dz/sm=r3/dm=r3 5, 31',
1317 'sv.extsw./pr=eq 5.v, 31',
1318 'sv.add. 5.v, 2.v, 1.v',
1319 'sv.add./m=r3 5.v, 2.v, 1.v',
1322 'sv.stw 5.v, 4(1.v)',
1323 'sv.ld 5.v, 4(1.v)',
1324 'setvl. 2, 3, 4, 0, 1, 1',
1325 'sv.setvl. 2, 3, 4, 0, 1, 1',
1328 "sv.stfsu 0.v, 16(4.v)",
1331 "sv.stfsu/els 0.v, 16(4)",
1334 'sv.add./mr 5.v, 2.v, 1.v',
1336 macros
= {'win2': '50', 'win': '60'}
1338 'sv.addi win2.v, win.v, -1',
1339 'sv.add./mrr 5.v, 2.v, 1.v',
1340 #'sv.lhzsh 5.v, 11(9.v), 15',
1341 #'sv.lwzsh 5.v, 11(9.v), 15',
1342 'sv.ffmadds 6.v, 2.v, 4.v, 6.v',
1345 #'sv.fmadds 0.v, 8.v, 16.v, 4.v',
1346 #'sv.ffadds 0.v, 8.v, 4.v',
1347 'svremap 11, 0, 1, 2, 3, 2, 1',
1348 'svshape 8, 1, 1, 1, 0',
1349 'svshape 8, 1, 1, 1, 1',
1352 #'sv.lfssh 4.v, 11(8.v), 15',
1353 #'sv.lwzsh 4.v, 11(8.v), 15',
1354 #'sv.svstep. 2.v, 4, 0',
1355 #'sv.fcfids. 48.v, 64.v',
1356 'sv.fcoss. 80.v, 0.v',
1357 'sv.fcoss. 20.v, 0.v',
1360 'sv.bc/all 3,12,192',
1361 'sv.bclr/vsbi 3,81.v,192',
1362 'sv.ld 5.v, 4(1.v)',
1363 'sv.svstep. 2.v, 4, 0',
1375 isa
= SVP64Asm(lst
, macros
=macros
)
1376 log("list", list(isa
))