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 LDST: https://libre-soc.org/openpower/sv/ldst/
15 Bugtracker: https://bugs.libre-soc.org/show_bug.cgi?id=578
20 from collections
import OrderedDict
22 from openpower
.decoder
.isa
.caller
import (SVP64PrefixFields
, SV64P_MAJOR_SIZE
,
23 SV64P_PID_SIZE
, SVP64RMFields
,
24 SVP64RM_EXTRA2_SPEC_SIZE
,
25 SVP64RM_EXTRA3_SPEC_SIZE
,
26 SVP64RM_MODE_SIZE
, SVP64RM_SMASK_SIZE
,
27 SVP64RM_MMODE_SIZE
, SVP64RM_MASK_SIZE
,
28 SVP64RM_SUBVL_SIZE
, SVP64RM_EWSRC_SIZE
,
30 from openpower
.decoder
.pseudo
.pagereader
import ISA
31 from openpower
.decoder
.power_svp64
import SVP64RM
, get_regtype
, decode_extra
32 from openpower
.decoder
.selectable_int
import SelectableInt
33 from openpower
.consts
import SVP64MODE
36 from openpower
.util
import log
39 # decode GPR into sv extra
40 def get_extra_gpr(etype
, regmode
, field
):
41 if regmode
== 'scalar':
42 # cut into 2-bits 5-bits SS FFFFF
44 field
= field
& 0b11111
46 # cut into 5-bits 2-bits FFFFF SS
47 sv_extra
= field
& 0b11
49 return sv_extra
, field
52 # decode 3-bit CR into sv extra
53 def get_extra_cr_3bit(etype
, regmode
, field
):
54 if regmode
== 'scalar':
55 # cut into 2-bits 3-bits SS FFF
59 # cut into 3-bits 4-bits FFF SSSS but will cut 2 zeros off later
60 sv_extra
= field
& 0b1111
62 return sv_extra
, field
66 def decode_subvl(encoding
):
67 pmap
= {'2': 0b01, '3': 0b10, '4': 0b11}
68 assert encoding
in pmap
, \
69 "encoding %s for SUBVL not recognised" % encoding
74 def decode_elwidth(encoding
):
75 pmap
= {'8': 0b11, '16': 0b10, '32': 0b01}
76 assert encoding
in pmap
, \
77 "encoding %s for elwidth not recognised" % encoding
81 # decodes predicate register encoding
82 def decode_predicate(encoding
):
93 'nl': (1, 0b001), 'ge': (1, 0b001), # same value
95 'ng': (1, 0b011), 'le': (1, 0b011), # same value
98 'so': (1, 0b110), 'un': (1, 0b110), # same value
99 'ns': (1, 0b111), 'nu': (1, 0b111), # same value
101 assert encoding
in pmap
, \
102 "encoding %s for predicate not recognised" % encoding
103 return pmap
[encoding
]
106 # decodes "Mode" in similar way to BO field (supposed to, anyway)
107 def decode_bo(encoding
):
108 pmap
= { # TODO: double-check that these are the same as Branch BO
110 'nl': 0b001, 'ge': 0b001, # same value
112 'ng': 0b011, 'le': 0b011, # same value
115 'so': 0b110, 'un': 0b110, # same value
116 'ns': 0b111, 'nu': 0b111, # same value
118 assert encoding
in pmap
, \
119 "encoding %s for BO Mode not recognised" % encoding
120 return pmap
[encoding
]
122 # partial-decode fail-first mode
125 def decode_ffirst(encoding
):
126 if encoding
in ['RC1', '~RC1']:
128 return decode_bo(encoding
)
131 def decode_reg(field
):
132 # decode the field number. "5.v" or "3.s" or "9"
133 field
= field
.split(".")
134 regmode
= 'scalar' # default
138 elif field
[1] == 'v':
140 field
= int(field
[0]) # actual register number
141 return field
, regmode
144 def decode_imm(field
):
145 ldst_imm
= "(" in field
and field
[-1] == ')'
147 return field
[:-1].split("(")
152 # decodes svp64 assembly listings and creates EXT001 svp64 prefixes
154 def __init__(self
, lst
, bigendian
=False, macros
=None):
159 self
.trans
= self
.translate(lst
)
160 self
.isa
= ISA() # reads the v3.0B pseudo-code markdown files
161 self
.svp64
= SVP64RM() # reads the svp64 Remap entries for registers
162 assert bigendian
== False, "error, bigendian not supported yet"
165 yield from self
.trans
167 def translate_one(self
, insn
, macros
=None):
170 macros
.update(self
.macros
)
173 # find first space, to get opcode
176 # now find opcode fields
177 fields
= ''.join(ls
[1:]).split(',')
178 mfields
= list(map(str.strip
, fields
))
179 log("opcode, fields", ls
, opcode
, mfields
)
182 for field
in mfields
:
183 fields
.append(macro_subst(macros
, field
))
184 log("opcode, fields substed", ls
, opcode
, fields
)
186 # sigh have to do setvl here manually for now...
187 # note the subtract one from SVi.
188 if opcode
in ["setvl", "setvl."]:
189 insn
= 22 << (31-5) # opcode 22, bits 0-5
190 fields
= list(map(int, fields
))
191 insn |
= fields
[0] << (31-10) # RT , bits 6-10
192 insn |
= fields
[1] << (31-15) # RA , bits 11-15
193 insn |
= (fields
[2]-1) << (31-22) # SVi , bits 16-22
194 insn |
= fields
[3] << (31-25) # ms , bit 25
195 insn |
= fields
[4] << (31-24) # vs , bit 24
196 insn |
= fields
[5] << (31-23) # vf , bit 23
197 insn |
= 0b00000 << (31-30) # XO , bits 26..30
198 if opcode
== 'setvl.':
199 insn |
= 1 << (31-31) # Rc=1 , bit 31
200 log("setvl", bin(insn
))
201 yield ".long 0x%x" % insn
204 # sigh have to do setvl here manually for now...
205 # note the subtract one from SVi.
206 if opcode
in ["svstep", "svstep."]:
207 insn
= 22 << (31-5) # opcode 22, bits 0-5
208 fields
= list(map(int, fields
))
209 insn |
= fields
[0] << (31-10) # RT , bits 6-10
210 insn |
= (fields
[1]-1) << (31-22) # SVi , bits 16-22
211 insn |
= fields
[2] << (31-25) # vf , bit 25
212 insn |
= 0b00011 << (31-30) # XO , bits 26..30
213 if opcode
== 'svstep.':
214 insn |
= 1 << (31-31) # Rc=1 , bit 31
215 log("svstep", bin(insn
))
216 yield ".long 0x%x" % insn
219 # and svshape. note that the dimension fields one subtracted from each
220 if opcode
== 'svshape':
221 insn
= 22 << (31-5) # opcode 22, bits 0-5
222 fields
= list(map(int, fields
))
223 insn |
= (fields
[0]-1) << (31-10) # SVxd , bits 6-10
224 insn |
= (fields
[1]-1) << (31-15) # SVyd , bits 11-15
225 insn |
= (fields
[2]-1) << (31-20) # SVzd , bits 16-20
226 insn |
= (fields
[3]) << (31-24) # SVRM , bits 21-24
227 insn |
= (fields
[4]) << (31-25) # vf , bits 25
228 insn |
= 0b00001 << (31-30) # XO , bits 26..30
230 log("svshape", bin(insn
))
231 yield ".long 0x%x" % insn
235 if opcode
== 'svremap':
236 insn
= 22 << (31-5) # opcode 22, bits 0-5
237 fields
= list(map(int, fields
))
238 insn |
= fields
[0] << (31-10) # SVme , bits 6-10
239 insn |
= fields
[1] << (31-12) # mi0 , bits 11-12
240 insn |
= fields
[2] << (31-14) # mi1 , bits 13-14
241 insn |
= fields
[3] << (31-16) # mi2 , bits 15-16
242 insn |
= fields
[4] << (31-18) # m00 , bits 17-18
243 insn |
= fields
[5] << (31-20) # m01 , bits 19-20
244 insn |
= fields
[6] << (31-21) # m01 , bit 21
245 insn |
= 0b00010 << (31-30) # XO , bits 26..30
247 log("svremap", bin(insn
))
248 yield ".long 0x%x" % insn
252 # XXX WARNING THESE ARE NOT APPROVED BY OPF ISA WG
253 # however we are out of space with opcode 22
254 if opcode
.startswith('fsins'):
255 fields
= list(map(int, fields
))
256 insn
= 59 << (31-5) # opcode 59, bits 0-5
257 insn |
= fields
[0] << (31-10) # RT , bits 6-10
258 insn |
= fields
[1] << (31-20) # RB , bits 16-20
259 insn |
= 0b1000001110 << (31-30) # XO , bits 21..30
260 if opcode
== 'fsins.':
261 insn |
= 1 << (31-31) # Rc=1 , bit 31
262 log("fsins", bin(insn
))
263 yield ".long 0x%x" % insn
267 # XXX WARNING THESE ARE NOT APPROVED BY OPF ISA WG
268 # however we are out of space with opcode 22
269 if opcode
.startswith('fcoss'):
270 fields
= list(map(int, fields
))
271 insn
= 59 << (31-5) # opcode 59, bits 0-5
272 insn |
= fields
[0] << (31-10) # RT , bits 6-10
273 insn |
= fields
[1] << (31-20) # RB , bits 16-20
274 insn |
= 0b1000101110 << (31-30) # XO , bits 21..30
275 if opcode
== 'fcoss.':
276 insn |
= 1 << (31-31) # Rc=1 , bit 31
277 log("fcoss", bin(insn
))
278 yield ".long 0x%x" % insn
281 # XXX WARNING THESE ARE NOT APPROVED BY OPF ISA WG
282 # however we are out of space with opcode 22
283 if opcode
== 'ternlogi':
291 instr
= (instr
<< 5) | rt
292 instr
= (instr
<< 5) | ra
293 instr
= (instr
<< 5) | rb
294 instr
= (instr
<< 8) | imm
295 instr
= (instr
<< 3) | xo
296 asm
= f
"ternlogi {rt}, {ra}, {rb}, {imm}"
297 yield f
".4byte {hex(instr)} # {asm}"
300 # XXX WARNING THESE ARE NOT APPROVED BY OPF ISA WG
301 # however we are out of space with opcode 22
302 if opcode
in ('grev', 'grevi', 'grevw', 'grevwi',
303 'grev.', 'grevi.', 'grevw.', 'grevwi.'):
305 # _ matches fields in table at:
306 # https://libre-soc.org/openpower/sv/bitmanip/
312 Rc
= 1 if '.' in opcode
else 0
315 rb_imm
= int(fields
[2])
317 instr
= (instr
<< 5) | rt
318 instr
= (instr
<< 5) | ra
319 if opcode
== 'grevi' or opcode
== 'grevi.':
320 assert 0 <= rb_imm
< 64
321 instr
= (instr
<< 6) | rb_imm
322 instr
= (instr
<< 9) | xo
324 assert 0 <= rb_imm
< 32
325 instr
= (instr
<< 5) | rb_imm
326 instr
= (instr
<< 10) | xo
327 instr
= (instr
<< 1) | Rc
328 asm
= f
"{opcode} {rt}, {ra}, {rb_imm}"
329 yield f
".4byte {hex(instr)} # {asm}"
332 # identify if is a svp64 mnemonic
333 if not opcode
.startswith('sv.'):
334 yield insn
# unaltered
336 opcode
= opcode
[3:] # strip leading "sv"
338 # start working on decoding the svp64 op: sv.basev30Bop/vec2/mode
339 opmodes
= opcode
.split("/") # split at "/"
340 v30b_op
= opmodes
.pop(0) # first is the v3.0B
341 # check instruction ends with dot
342 rc_mode
= v30b_op
.endswith('.')
344 v30b_op
= v30b_op
[:-1]
346 # sigh again, have to recognised LD/ST bit-reverse instructions
347 # this has to be "processed" to fit into a v3.0B without the "sh"
348 # e.g. ldsh is actually ld
349 ldst_shift
= v30b_op
.startswith("l") and v30b_op
.endswith("sh")
351 if v30b_op
not in isa
.instr
:
352 raise Exception("opcode %s of '%s' not supported" %
356 # okaay we need to process the fields and make this:
357 # ldsh RT, SVD(RA), RC - 11 bits for SVD, 5 for RC
359 # ld RT, D(RA) - 16 bits
360 # likewise same for SVDS (9 bits for SVDS, 5 for RC, 14 bits for DS)
361 form
= isa
.instr
[v30b_op
].form
# get form (SVD-Form, SVDS-Form)
365 # identify if this is a ld/st immediate(reg) thing
366 ldst_imm
= "(" in field
and field
[-1] == ')'
368 newfields
.append(field
[:-1].split("("))
370 newfields
.append(field
)
372 immed
, RA
= newfields
[1]
374 RC
= int(newfields
.pop(2)) # better be an integer number!
375 if form
== 'SVD': # 16 bit: immed 11 bits, RC shift up 11
376 immed
= (immed
& 0b11111111111) |
(RC
<< 11)
377 if immed
& (1 << 15): # should be negative
379 if form
== 'SVDS': # 14 bit: immed 9 bits, RC shift up 9
380 immed
= (immed
& 0b111111111) |
(RC
<< 9)
381 if immed
& (1 << 13): # should be negative
383 newfields
[1] = "%d(%s)" % (immed
, RA
)
386 # and strip off "sh" from end, and add "sh" to opmodes, instead
387 v30b_op
= v30b_op
[:-2]
389 log("rewritten", v30b_op
, opmodes
, fields
)
391 if v30b_op
not in svp64
.instrs
:
392 raise Exception("opcode %s of '%s' not an svp64 instruction" %
394 v30b_regs
= isa
.instr
[v30b_op
].regs
[0] # get regs info "RT, RA, RB"
395 rm
= svp64
.instrs
[v30b_op
] # one row of the svp64 RM CSV
396 log("v3.0B op", v30b_op
, "Rc=1" if rc_mode
else '')
397 log("v3.0B regs", opcode
, v30b_regs
)
400 # right. the first thing to do is identify the ordering of
401 # the registers, by name. the EXTRA2/3 ordering is in
402 # rm['0']..rm['3'] but those fields contain the names RA, BB
403 # etc. we have to read the pseudocode to understand which
404 # reg is which in our instruction. sigh.
406 # first turn the svp64 rm into a "by name" dict, recording
407 # which position in the RM EXTRA it goes into
408 # also: record if the src or dest was a CR, for sanity-checking
409 # (elwidth overrides on CRs are banned)
410 decode
= decode_extra(rm
)
411 dest_reg_cr
, src_reg_cr
, svp64_src
, svp64_dest
= decode
413 log("EXTRA field index, src", svp64_src
)
414 log("EXTRA field index, dest", svp64_dest
)
416 # okaaay now we identify the field value (opcode N,N,N) with
417 # the pseudo-code info (opcode RT, RA, RB)
418 assert len(fields
) == len(v30b_regs
), \
419 "length of fields %s must match insn `%s` fields %s" % \
420 (str(v30b_regs
), insn
, str(fields
))
421 opregfields
= zip(fields
, v30b_regs
) # err that was easy
423 # now for each of those find its place in the EXTRA encoding
424 # note there is the possibility (for LD/ST-with-update) of
425 # RA occurring **TWICE**. to avoid it getting added to the
426 # v3.0B suffix twice, we spot it as a duplicate, here
427 extras
= OrderedDict()
428 for idx
, (field
, regname
) in enumerate(opregfields
):
429 imm
, regname
= decode_imm(regname
)
430 rtype
= get_regtype(regname
)
431 log(" idx find", rtype
, idx
, field
, regname
, imm
)
433 # probably an immediate field, append it straight
434 extras
[('imm', idx
, False)] = (idx
, field
, None, None, None)
436 extra
= svp64_src
.get(regname
, None)
437 if extra
is not None:
438 extra
= ('s', extra
, False) # not a duplicate
439 extras
[extra
] = (idx
, field
, regname
, rtype
, imm
)
440 log(" idx src", idx
, extra
, extras
[extra
])
441 dextra
= svp64_dest
.get(regname
, None)
442 log("regname in", regname
, dextra
)
443 if dextra
is not None:
444 is_a_duplicate
= extra
is not None # duplicate spotted
445 dextra
= ('d', dextra
, is_a_duplicate
)
446 extras
[dextra
] = (idx
, field
, regname
, rtype
, imm
)
447 log(" idx dst", idx
, extra
, extras
[dextra
])
449 # great! got the extra fields in their associated positions:
450 # also we know the register type. now to create the EXTRA encodings
451 etype
= rm
['Etype'] # Extra type: EXTRA3/EXTRA2
452 ptype
= rm
['Ptype'] # Predication type: Twin / Single
455 for extra_idx
, (idx
, field
, rname
, rtype
, iname
) in extras
.items():
456 # is it a field we don't alter/examine? if so just put it
459 v30b_newfields
.append(field
)
462 # identify if this is a ld/st immediate(reg) thing
463 ldst_imm
= "(" in field
and field
[-1] == ')'
465 immed
, field
= field
[:-1].split("(")
467 field
, regmode
= decode_reg(field
)
468 log(" ", extra_idx
, rname
, rtype
,
469 regmode
, iname
, field
, end
=" ")
471 # see Mode field https://libre-soc.org/openpower/sv/svp64/
472 # XXX TODO: the following is a bit of a laborious repeated
473 # mess, which could (and should) easily be parameterised.
474 # XXX also TODO: the LD/ST modes which are different
475 # https://libre-soc.org/openpower/sv/ldst/
477 # encode SV-GPR and SV-FPR field into extra, v3.0field
478 if rtype
in ['GPR', 'FPR']:
479 sv_extra
, field
= get_extra_gpr(etype
, regmode
, field
)
480 # now sanity-check. EXTRA3 is ok, EXTRA2 has limits
481 # (and shrink to a single bit if ok)
482 if etype
== 'EXTRA2':
483 if regmode
== 'scalar':
484 # range is r0-r63 in increments of 1
485 assert (sv_extra
>> 1) == 0, \
486 "scalar GPR %s cannot fit into EXTRA2 %s" % \
487 (rname
, str(extras
[extra_idx
]))
488 # all good: encode as scalar
489 sv_extra
= sv_extra
& 0b01
491 # range is r0-r127 in increments of 4
492 assert sv_extra
& 0b01 == 0, \
493 "%s: vector field %s cannot fit " \
495 (insn
, rname
, str(extras
[extra_idx
]))
496 # all good: encode as vector (bit 2 set)
497 sv_extra
= 0b10 |
(sv_extra
>> 1)
498 elif regmode
== 'vector':
499 # EXTRA3 vector bit needs marking
502 # encode SV-CR 3-bit field into extra, v3.0field
503 elif rtype
== 'CR_3bit':
504 sv_extra
, field
= get_extra_cr_3bit(etype
, regmode
, field
)
505 # now sanity-check (and shrink afterwards)
506 if etype
== 'EXTRA2':
507 if regmode
== 'scalar':
508 # range is CR0-CR15 in increments of 1
509 assert (sv_extra
>> 1) == 0, \
510 "scalar CR %s cannot fit into EXTRA2 %s" % \
511 (rname
, str(extras
[extra_idx
]))
512 # all good: encode as scalar
513 sv_extra
= sv_extra
& 0b01
515 # range is CR0-CR127 in increments of 16
516 assert sv_extra
& 0b111 == 0, \
517 "vector CR %s cannot fit into EXTRA2 %s" % \
518 (rname
, str(extras
[extra_idx
]))
519 # all good: encode as vector (bit 2 set)
520 sv_extra
= 0b10 |
(sv_extra
>> 3)
522 if regmode
== 'scalar':
523 # range is CR0-CR31 in increments of 1
524 assert (sv_extra
>> 2) == 0, \
525 "scalar CR %s cannot fit into EXTRA2 %s" % \
526 (rname
, str(extras
[extra_idx
]))
527 # all good: encode as scalar
528 sv_extra
= sv_extra
& 0b11
530 # range is CR0-CR127 in increments of 8
531 assert sv_extra
& 0b11 == 0, \
532 "vector CR %s cannot fit into EXTRA2 %s" % \
533 (rname
, str(extras
[extra_idx
]))
534 # all good: encode as vector (bit 3 set)
535 sv_extra
= 0b100 |
(sv_extra
>> 2)
537 # encode SV-CR 5-bit field into extra, v3.0field
538 # *sigh* this is the same as 3-bit except the 2 LSBs are
540 elif rtype
== 'CR_5bit':
541 cr_subfield
= field
& 0b11
542 field
= field
>> 2 # strip bottom 2 bits
543 sv_extra
, field
= get_extra_cr_3bit(etype
, regmode
, field
)
544 # now sanity-check (and shrink afterwards)
545 if etype
== 'EXTRA2':
546 if regmode
== 'scalar':
547 # range is CR0-CR15 in increments of 1
548 assert (sv_extra
>> 1) == 0, \
549 "scalar CR %s cannot fit into EXTRA2 %s" % \
550 (rname
, str(extras
[extra_idx
]))
551 # all good: encode as scalar
552 sv_extra
= sv_extra
& 0b01
554 # range is CR0-CR127 in increments of 16
555 assert sv_extra
& 0b111 == 0, \
556 "vector CR %s cannot fit into EXTRA2 %s" % \
557 (rname
, str(extras
[extra_idx
]))
558 # all good: encode as vector (bit 2 set)
559 sv_extra
= 0b10 |
(sv_extra
>> 3)
561 if regmode
== 'scalar':
562 # range is CR0-CR31 in increments of 1
563 assert (sv_extra
>> 2) == 0, \
564 "scalar CR %s cannot fit into EXTRA2 %s" % \
565 (rname
, str(extras
[extra_idx
]))
566 # all good: encode as scalar
567 sv_extra
= sv_extra
& 0b11
569 # range is CR0-CR127 in increments of 8
570 assert sv_extra
& 0b11 == 0, \
571 "vector CR %s cannot fit into EXTRA3 %s" % \
572 (rname
, str(extras
[extra_idx
]))
573 # all good: encode as vector (bit 3 set)
574 sv_extra
= 0b100 |
(sv_extra
>> 2)
575 # reconstruct the actual 5-bit CR field
576 field
= (field
<< 2) | cr_subfield
579 print("no type match", rtype
)
581 # capture the extra field info
582 log("=>", "%5s" % bin(sv_extra
), field
)
583 extras
[extra_idx
] = sv_extra
585 # append altered field value to v3.0b, differs for LDST
586 # note that duplicates are skipped e.g. EXTRA2 contains
587 # *BOTH* s:RA *AND* d:RA which happens on LD/ST-with-update
588 srcdest
, idx
, duplicate
= extra_idx
589 if duplicate
: # skip adding to v3.0b fields, already added
592 v30b_newfields
.append(("%s(%s)" % (immed
, str(field
))))
594 v30b_newfields
.append(str(field
))
596 log("new v3.0B fields", v30b_op
, v30b_newfields
)
597 log("extras", extras
)
599 # rright. now we have all the info. start creating SVP64 RM
600 svp64_rm
= SVP64RMFields()
602 # begin with EXTRA fields
603 for idx
, sv_extra
in extras
.items():
609 srcdest
, idx
, duplicate
= idx
610 if etype
== 'EXTRA2':
611 svp64_rm
.extra2
[idx
].eq(
612 SelectableInt(sv_extra
, SVP64RM_EXTRA2_SPEC_SIZE
))
614 svp64_rm
.extra3
[idx
].eq(
615 SelectableInt(sv_extra
, SVP64RM_EXTRA3_SPEC_SIZE
))
617 # identify if the op is a LD/ST. the "blegh" way. copied
618 # from power_enums. TODO, split the list _insns down.
620 "lbarx", "lbz", "lbzu", "lbzux", "lbzx", # load byte
621 "ld", "ldarx", "ldbrx", "ldu", "ldux", "ldx", # load double
622 "lfs", "lfsx", "lfsu", "lfsux", # FP load single
623 "lfd", "lfdx", "lfdu", "lfdux", "lfiwzx", "lfiwax", # FP load double
624 "lha", "lharx", "lhau", "lhaux", "lhax", # load half
625 "lhbrx", "lhz", "lhzu", "lhzux", "lhzx", # more load half
626 "lwa", "lwarx", "lwaux", "lwax", "lwbrx", # load word
627 "lwz", "lwzcix", "lwzu", "lwzux", "lwzx", # more load word
630 "stb", "stbcix", "stbcx", "stbu", "stbux", "stbx",
631 "std", "stdbrx", "stdcx", "stdu", "stdux", "stdx",
632 "stfs", "stfsx", "stfsu", "stfux", # FP store single
633 "stfd", "stfdx", "stfdu", "stfdux", "stfiwx", # FP store double
634 "sth", "sthbrx", "sthcx", "sthu", "sthux", "sthx",
635 "stw", "stwbrx", "stwcx", "stwu", "stwux", "stwx",
637 # use this to determine if the SVP64 RM format is different.
638 # see https://libre-soc.org/openpower/sv/ldst/
639 is_ldst
= is_ld
or is_st
641 # branch-conditional detection
649 destwid
= 0 # bits 4-5
650 srcwid
= 0 # bits 6-7
652 smask
= 0 # bits 16-18 but only for twin-predication
653 mode
= 0 # bits 19-23
655 mask_m_specified
= False
666 mapreduce_crm
= False
667 mapreduce_svm
= False
673 # branch-conditional bits
683 # ok let's start identifying opcode augmentation fields
684 for encmode
in opmodes
:
685 # predicate mask (src and dest)
686 if encmode
.startswith("m="):
688 pmmode
, pmask
= decode_predicate(encmode
[2:])
689 smmode
, smask
= pmmode
, pmask
691 mask_m_specified
= True
692 # predicate mask (dest)
693 elif encmode
.startswith("dm="):
695 pmmode
, pmask
= decode_predicate(encmode
[3:])
698 # predicate mask (src, twin-pred)
699 elif encmode
.startswith("sm="):
701 smmode
, smask
= decode_predicate(encmode
[3:])
705 elif encmode
.startswith("sh"):
708 elif encmode
.startswith("vec"):
709 subvl
= decode_subvl(encmode
[3:])
711 elif encmode
.startswith("ew="):
712 destwid
= decode_elwidth(encmode
[3:])
713 elif encmode
.startswith("sw="):
714 srcwid
= decode_elwidth(encmode
[3:])
715 # element-strided LD/ST
716 elif encmode
== 'els':
719 elif encmode
== 'sats':
720 assert sv_mode
is None
723 elif encmode
== 'satu':
724 assert sv_mode
is None
728 elif encmode
== 'sz':
730 elif encmode
== 'dz':
733 elif encmode
.startswith("ff="):
734 assert sv_mode
is None
736 failfirst
= decode_ffirst(encmode
[3:])
737 # predicate-result, interestingly same as fail-first
738 elif encmode
.startswith("pr="):
739 assert sv_mode
is None
741 predresult
= decode_ffirst(encmode
[3:])
742 # map-reduce mode, reverse-gear
743 elif encmode
== 'mrr':
744 assert sv_mode
is None
749 elif encmode
== 'mr':
750 assert sv_mode
is None
753 elif encmode
== 'crm': # CR on map-reduce
754 assert sv_mode
is None
757 elif encmode
== 'svm': # sub-vector mode
762 elif encmode
== 'st': # svstep mode
764 elif encmode
== 'sr': # svstep BRc mode
767 elif encmode
== 'vs': # VLSET mode
769 elif encmode
== 'vsi': # VLSET mode with VLI (VL inclusives)
772 elif encmode
== 'vsb': # VLSET mode with VSb
775 elif encmode
== 'vsbi': # VLSET mode with VLI and VSb
779 elif encmode
== 'snz': # sz (only) already set above
782 elif encmode
== 'lu': # LR update mode
785 raise AssertionError("unknown encmode %s" % encmode
)
787 raise AssertionError("unknown encmode %s" % encmode
)
790 # since m=xx takes precedence (overrides) sm=xx and dm=xx,
791 # treat them as mutually exclusive
793 assert not has_smask
,\
794 "cannot have both source-mask and predicate mask"
795 assert not has_pmask
,\
796 "cannot have both dest-mask and predicate mask"
797 # since the default is INT predication (ALWAYS), if you
798 # specify one CR mask, you must specify both, to avoid
799 # mixing INT and CR reg types
800 if has_pmask
and pmmode
== 1:
802 "need explicit source-mask in CR twin predication"
803 if has_smask
and smmode
== 1:
805 "need explicit dest-mask in CR twin predication"
806 # sanity-check that 2Pred mask is same mode
807 if has_pmask
and has_smask
:
808 assert smmode
== pmmode
, \
809 "predicate masks %s and %s must be same reg type" % \
812 # sanity-check that twin-predication mask only specified in 2P mode
814 assert not has_smask
, \
815 "source-mask can only be specified on Twin-predicate ops"
816 assert not has_pmask
, \
817 "dest-mask can only be specified on Twin-predicate ops"
819 # construct the mode field, doing sanity-checking along the way
821 assert sv_mode
== 0b00, "sub-vector mode in mapreduce only"
822 assert subvl
!= 0, "sub-vector mode not possible on SUBVL=1"
825 assert has_smask
or mask_m_specified
, \
826 "src zeroing requires a source predicate"
828 assert has_pmask
or mask_m_specified
, \
829 "dest zeroing requires a dest predicate"
831 # check LDST shifted, only available in "normal" mode
832 if is_ldst
and ldst_shift
:
833 assert sv_mode
is None, \
834 "LD shift cannot have modes (%s) applied" % sv_mode
836 # now create mode and (overridden) src/dst widths
837 # XXX TODO: sanity-check bc modes
839 sv_mode
= ((bc_svstep
<< SVP64MODE
.MOD2_MSB
) |
840 (bc_vlset
<< SVP64MODE
.MOD2_LSB
) |
841 (bc_snz
<< SVP64MODE
.BC_SNZ
))
842 srcwid
= (bc_vsb
<< 1) | bc_lru
843 destwid
= (bc_lru
<< 1) | bc_all
847 ######################################
850 mode |
= src_zero
<< SVP64MODE
.SZ
# predicate zeroing
851 mode |
= dst_zero
<< SVP64MODE
.DZ
# predicate zeroing
853 # TODO: for now, LD/ST-indexed is ignored.
854 mode |
= ldst_elstride
<< SVP64MODE
.ELS_NORMAL
# el-strided
857 mode |
= 1 << SVP64MODE
.LDST_SHIFT
859 # TODO, reduce and subvector mode
860 # 00 1 dz CRM reduce mode (mapreduce), SUBVL=1
861 # 00 1 SVM CRM subvector reduce mode, SUBVL>1
865 ######################################
867 elif sv_mode
== 0b00:
868 mode |
= (0b1 << SVP64MODE
.REDUCE
) # sets mapreduce
869 assert dst_zero
== 0, "dest-zero not allowed in mapreduce mode"
871 mode |
= (0b1 << SVP64MODE
.RG
) # sets Reverse-gear mode
873 mode |
= (0b1 << SVP64MODE
.CRM
) # sets CRM mode
874 assert rc_mode
, "CRM only allowed when Rc=1"
875 # bit of weird encoding to jam zero-pred or SVM mode in.
876 # SVM mode can be enabled only when SUBVL=2/3/4 (vec2/3/4)
878 mode |
= dst_zero
<< SVP64MODE
.DZ
# predicate zeroing
880 mode |
= (0b1 << SVP64MODE
.SVM
) # sets SVM mode
882 ######################################
884 elif sv_mode
== 0b01:
885 assert src_zero
== 0, "dest-zero not allowed in failfirst mode"
886 if failfirst
== 'RC1':
887 mode |
= (0b1 << SVP64MODE
.RC1
) # sets RC1 mode
888 mode |
= (dst_zero
<< SVP64MODE
.DZ
) # predicate dst-zeroing
889 assert rc_mode
== False, "ffirst RC1 only possible when Rc=0"
890 elif failfirst
== '~RC1':
891 mode |
= (0b1 << SVP64MODE
.RC1
) # sets RC1 mode
892 mode |
= (dst_zero
<< SVP64MODE
.DZ
) # predicate dst-zeroing
893 mode |
= (0b1 << SVP64MODE
.INV
) # ... with inversion
894 assert rc_mode
== False, "ffirst RC1 only possible when Rc=0"
896 assert dst_zero
== 0, "dst-zero not allowed in ffirst BO"
897 assert rc_mode
, "ffirst BO only possible when Rc=1"
898 mode |
= (failfirst
<< SVP64MODE
.BO_LSB
) # set BO
900 ######################################
902 elif sv_mode
== 0b10:
903 mode |
= src_zero
<< SVP64MODE
.SZ
# predicate zeroing
904 mode |
= dst_zero
<< SVP64MODE
.DZ
# predicate zeroing
905 mode |
= (saturation
<< SVP64MODE
.N
) # signed/us saturation
907 ######################################
908 # "predicate-result" modes. err... code-duplication from ffirst
909 elif sv_mode
== 0b11:
910 assert src_zero
== 0, "dest-zero not allowed in predresult mode"
911 if predresult
== 'RC1':
912 mode |
= (0b1 << SVP64MODE
.RC1
) # sets RC1 mode
913 mode |
= (dst_zero
<< SVP64MODE
.DZ
) # predicate dst-zeroing
914 assert rc_mode
== False, "pr-mode RC1 only possible when Rc=0"
915 elif predresult
== '~RC1':
916 mode |
= (0b1 << SVP64MODE
.RC1
) # sets RC1 mode
917 mode |
= (dst_zero
<< SVP64MODE
.DZ
) # predicate dst-zeroing
918 mode |
= (0b1 << SVP64MODE
.INV
) # ... with inversion
919 assert rc_mode
== False, "pr-mode RC1 only possible when Rc=0"
921 assert dst_zero
== 0, "dst-zero not allowed in pr-mode BO"
922 assert rc_mode
, "pr-mode BO only possible when Rc=1"
923 mode |
= (predresult
<< SVP64MODE
.BO_LSB
) # set BO
925 # whewww.... modes all done :)
926 # now put into svp64_rm
929 svp64_rm
.mode
.eq(SelectableInt(mode
, SVP64RM_MODE_SIZE
))
931 # put in predicate masks into svp64_rm
933 # source pred: bits 16-18
934 svp64_rm
.smask
.eq(SelectableInt(smask
, SVP64RM_SMASK_SIZE
))
936 svp64_rm
.mmode
.eq(SelectableInt(mmode
, SVP64RM_MMODE_SIZE
))
938 svp64_rm
.mask
.eq(SelectableInt(pmask
, SVP64RM_MASK_SIZE
))
940 # and subvl: bits 8-9
941 svp64_rm
.subvl
.eq(SelectableInt(subvl
, SVP64RM_SUBVL_SIZE
))
945 svp64_rm
.ewsrc
.eq(SelectableInt(srcwid
, SVP64RM_EWSRC_SIZE
))
947 svp64_rm
.elwidth
.eq(SelectableInt(destwid
, SVP64RM_ELWIDTH_SIZE
))
949 # nice debug printout. (and now for something completely different)
950 # https://youtu.be/u0WOIwlXE9g?t=146
951 svp64_rm_value
= svp64_rm
.spr
.value
952 log("svp64_rm", hex(svp64_rm_value
), bin(svp64_rm_value
))
953 log(" mmode 0 :", bin(mmode
))
954 log(" pmask 1-3 :", bin(pmask
))
955 log(" dstwid 4-5 :", bin(destwid
))
956 log(" srcwid 6-7 :", bin(srcwid
))
957 log(" subvl 8-9 :", bin(subvl
))
958 log(" mode 19-23:", bin(mode
))
959 offs
= 2 if etype
== 'EXTRA2' else 3 # 2 or 3 bits
960 for idx
, sv_extra
in extras
.items():
965 srcdest
, idx
, duplicate
= idx
966 start
= (10+idx
*offs
)
968 log(" extra%d %2d-%2d:" % (idx
, start
, end
),
971 log(" smask 16-17:", bin(smask
))
974 # first, construct the prefix from its subfields
975 svp64_prefix
= SVP64PrefixFields()
976 svp64_prefix
.major
.eq(SelectableInt(0x1, SV64P_MAJOR_SIZE
))
977 svp64_prefix
.pid
.eq(SelectableInt(0b11, SV64P_PID_SIZE
))
978 svp64_prefix
.rm
.eq(svp64_rm
.spr
)
980 # fiinally yield the svp64 prefix and the thingy. v3.0b opcode
981 rc
= '.' if rc_mode
else ''
982 yield ".long 0x%x" % svp64_prefix
.insn
.value
984 # argh, sv.fmadds etc. need to be done manually
985 if v30b_op
== 'ffmadds':
986 opcode
= 59 << (32-6) # bits 0..6 (MSB0)
987 opcode |
= int(v30b_newfields
[0]) << (32-11) # FRT
988 opcode |
= int(v30b_newfields
[1]) << (32-16) # FRA
989 opcode |
= int(v30b_newfields
[2]) << (32-21) # FRB
990 opcode |
= int(v30b_newfields
[3]) << (32-26) # FRC
991 opcode |
= 0b00101 << (32-31) # bits 26-30
993 opcode |
= 1 # Rc, bit 31.
994 yield ".long 0x%x" % opcode
995 # argh, sv.fdmadds need to be done manually
996 elif v30b_op
== 'fdmadds':
997 opcode
= 59 << (32-6) # bits 0..6 (MSB0)
998 opcode |
= int(v30b_newfields
[0]) << (32-11) # FRT
999 opcode |
= int(v30b_newfields
[1]) << (32-16) # FRA
1000 opcode |
= int(v30b_newfields
[2]) << (32-21) # FRB
1001 opcode |
= int(v30b_newfields
[3]) << (32-26) # FRC
1002 opcode |
= 0b01111 << (32-31) # bits 26-30
1004 opcode |
= 1 # Rc, bit 31.
1005 yield ".long 0x%x" % opcode
1006 # argh, sv.ffadds etc. need to be done manually
1007 elif v30b_op
== 'ffadds':
1008 opcode
= 59 << (32-6) # bits 0..6 (MSB0)
1009 opcode |
= int(v30b_newfields
[0]) << (32-11) # FRT
1010 opcode |
= int(v30b_newfields
[1]) << (32-16) # FRA
1011 opcode |
= int(v30b_newfields
[2]) << (32-21) # FRB
1012 opcode |
= 0b01101 << (32-31) # bits 26-30
1014 opcode |
= 1 # Rc, bit 31.
1015 yield ".long 0x%x" % opcode
1016 # sigh have to do svstep here manually for now...
1017 elif opcode
in ["svstep", "svstep."]:
1018 insn
= 22 << (31-5) # opcode 22, bits 0-5
1019 insn |
= int(v30b_newfields
[0]) << (31-10) # RT , bits 6-10
1020 insn |
= int(v30b_newfields
[1]) << (31-22) # SVi , bits 16-22
1021 insn |
= int(v30b_newfields
[2]) << (31-25) # vf , bit 25
1022 insn |
= 0b00011 << (31-30) # XO , bits 26..30
1023 if opcode
== 'svstep.':
1024 insn |
= 1 << (31-31) # Rc=1 , bit 31
1025 log("svstep", bin(insn
))
1026 yield ".long 0x%x" % insn
1028 elif v30b_op
in ["setvl", "setvl."]:
1029 insn
= 22 << (31-5) # opcode 22, bits 0-5
1030 fields
= list(map(int, fields
))
1031 insn |
= fields
[0] << (31-10) # RT , bits 6-10
1032 insn |
= fields
[1] << (31-15) # RA , bits 11-15
1033 insn |
= (fields
[2]-1) << (31-22) # SVi , bits 16-22
1034 insn |
= fields
[3] << (31-25) # ms , bit 25
1035 insn |
= fields
[4] << (31-24) # vs , bit 24
1036 insn |
= fields
[5] << (31-23) # vf , bit 23
1037 insn |
= 0b00000 << (31-30) # XO , bits 26..30
1038 if opcode
== 'setvl.':
1039 insn |
= 1 << (31-31) # Rc=1 , bit 31
1040 log("setvl", bin(insn
))
1041 yield ".long 0x%x" % insn
1043 elif v30b_op
in ["fcoss", "fcoss."]:
1044 insn
= 59 << (31-5) # opcode 59, bits 0-5
1045 insn |
= int(v30b_newfields
[0]) << (31-10) # RT , bits 6-10
1046 insn |
= int(v30b_newfields
[1]) << (31-20) # RB , bits 16-20
1047 insn |
= 0b1000101110 << (31-30) # XO , bits 21..30
1048 if opcode
== 'fcoss.':
1049 insn |
= 1 << (31-31) # Rc=1 , bit 31
1050 log("fcoss", bin(insn
))
1051 yield ".long 0x%x" % insn
1054 yield "%s %s" % (v30b_op
+rc
, ", ".join(v30b_newfields
))
1055 log("new v3.0B fields", v30b_op
, v30b_newfields
)
1057 def translate(self
, lst
):
1059 yield from self
.translate_one(insn
)
1062 def macro_subst(macros
, txt
):
1064 print("subst", txt
, macros
)
1067 for macro
, value
in macros
.items():
1070 replaced
= txt
.replace(macro
, value
)
1071 print("macro", txt
, "replaced", replaced
, macro
, value
)
1074 toreplace
= '%s.s' % macro
1075 if toreplace
== txt
:
1077 replaced
= txt
.replace(toreplace
, "%s.s" % value
)
1078 print("macro", txt
, "replaced", replaced
, toreplace
, value
)
1081 toreplace
= '%s.v' % macro
1082 if toreplace
== txt
:
1084 replaced
= txt
.replace(toreplace
, "%s.v" % value
)
1085 print("macro", txt
, "replaced", replaced
, toreplace
, value
)
1088 toreplace
= '(%s)' % macro
1089 if toreplace
in txt
:
1091 replaced
= txt
.replace(toreplace
, '(%s)' % value
)
1092 print("macro", txt
, "replaced", replaced
, toreplace
, value
)
1095 print(" processed", txt
)
1103 if not line
[0].isspace():
1112 # get an input file and an output file
1116 outfile
= sys
.stdout
1117 # read the whole lot in advance in case of in-place
1118 lines
= list(infile
.readlines())
1119 elif len(args
) != 2:
1120 print("pysvp64asm [infile | -] [outfile | -]")
1126 infile
= open(args
[0], "r")
1127 # read the whole lot in advance in case of in-place overwrite
1128 lines
= list(infile
.readlines())
1131 outfile
= sys
.stdout
1133 outfile
= open(args
[1], "w")
1135 # read the line, look for "sv", process it
1136 macros
= {} # macros which start ".set"
1139 ls
= line
.split("#")
1142 if op
.startswith("setvl") or op
.startswith("svshape"):
1143 ws
, line
= get_ws(ls
[0])
1144 lst
= list(isa
.translate_one(ls
[0].strip(), macros
))
1145 lst
= '; '.join(lst
)
1146 outfile
.write("%s%s # %s\n" % (ws
, lst
, ls
[0]))
1148 if ls
[0].startswith(".set"):
1149 macro
= ls
[0][4:].split(",")
1150 macro
, value
= list(map(str.strip
, macro
))
1151 macros
[macro
] = value
1155 potential
= ls
[1].strip()
1156 if not potential
.startswith("sv."):
1160 ws
, line
= get_ws(line
)
1161 # SV line indentified
1162 lst
= list(isa
.translate_one(potential
, macros
))
1163 lst
= '; '.join(lst
)
1164 outfile
.write("%s%s # %s\n" % (ws
, lst
, potential
))
1167 if __name__
== '__main__':
1168 lst
= ['slw 3, 1, 4',
1171 'sv.cmpi 5, 1, 3, 2',
1173 'sv.isel 64.v, 3, 2, 65.v',
1174 'sv.setb/dm=r3/sm=1<<r3 5, 31',
1175 'sv.setb/m=r3 5, 31',
1176 'sv.setb/vec2 5, 31',
1177 'sv.setb/sw=8/ew=16 5, 31',
1178 'sv.extsw./ff=eq 5, 31',
1179 'sv.extsw./satu/sz/dz/sm=r3/dm=r3 5, 31',
1180 'sv.extsw./pr=eq 5.v, 31',
1181 'sv.add. 5.v, 2.v, 1.v',
1182 'sv.add./m=r3 5.v, 2.v, 1.v',
1185 'sv.stw 5.v, 4(1.v)',
1186 'sv.ld 5.v, 4(1.v)',
1187 'setvl. 2, 3, 4, 0, 1, 1',
1188 'sv.setvl. 2, 3, 4, 0, 1, 1',
1191 "sv.stfsu 0.v, 16(4.v)",
1194 "sv.stfsu/els 0.v, 16(4)",
1197 'sv.add./mr 5.v, 2.v, 1.v',
1199 macros
= {'win2': '50', 'win': '60'}
1201 'sv.addi win2.v, win.v, -1',
1202 'sv.add./mrr 5.v, 2.v, 1.v',
1203 #'sv.lhzsh 5.v, 11(9.v), 15',
1204 #'sv.lwzsh 5.v, 11(9.v), 15',
1205 'sv.ffmadds 6.v, 2.v, 4.v, 6.v',
1208 #'sv.fmadds 0.v, 8.v, 16.v, 4.v',
1209 #'sv.ffadds 0.v, 8.v, 4.v',
1210 'svremap 11, 0, 1, 2, 3, 2, 1',
1211 'svshape 8, 1, 1, 1, 0',
1212 'svshape 8, 1, 1, 1, 1',
1215 #'sv.lfssh 4.v, 11(8.v), 15',
1216 #'sv.lwzsh 4.v, 11(8.v), 15',
1217 #'sv.svstep. 2.v, 4, 0',
1218 #'sv.fcfids. 48.v, 64.v',
1219 'sv.fcoss. 80.v, 0.v',
1220 'sv.fcoss. 20.v, 0.v',
1223 'sv.bc/all 3,12,192',
1224 'sv.bclr/vsbi 3,81.v,192',
1226 isa
= SVP64Asm(lst
, macros
=macros
)
1227 print("list", list(isa
))