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
19 from collections
import OrderedDict
21 from openpower
.decoder
.isa
.caller
import (SVP64PrefixFields
, SV64P_MAJOR_SIZE
,
22 SV64P_PID_SIZE
, SVP64RMFields
,
23 SVP64RM_EXTRA2_SPEC_SIZE
,
24 SVP64RM_EXTRA3_SPEC_SIZE
,
25 SVP64RM_MODE_SIZE
, SVP64RM_SMASK_SIZE
,
26 SVP64RM_MMODE_SIZE
, SVP64RM_MASK_SIZE
,
27 SVP64RM_SUBVL_SIZE
, SVP64RM_EWSRC_SIZE
,
29 from openpower
.decoder
.pseudo
.pagereader
import ISA
30 from openpower
.decoder
.power_svp64
import SVP64RM
, get_regtype
, decode_extra
31 from openpower
.decoder
.selectable_int
import SelectableInt
32 from openpower
.consts
import SVP64MODE
35 from openpower
.util
import log
38 # decode GPR into sv extra
39 def get_extra_gpr(etype
, regmode
, field
):
40 if regmode
== 'scalar':
41 # cut into 2-bits 5-bits SS FFFFF
43 field
= field
& 0b11111
45 # cut into 5-bits 2-bits FFFFF SS
46 sv_extra
= field
& 0b11
48 return sv_extra
, field
51 # decode 3-bit CR into sv extra
52 def get_extra_cr_3bit(etype
, regmode
, field
):
53 if regmode
== 'scalar':
54 # cut into 2-bits 3-bits SS FFF
58 # cut into 3-bits 4-bits FFF SSSS but will cut 2 zeros off later
59 sv_extra
= field
& 0b1111
61 return sv_extra
, field
65 def decode_subvl(encoding
):
66 pmap
= {'2': 0b01, '3': 0b10, '4': 0b11}
67 assert encoding
in pmap
, \
68 "encoding %s for SUBVL not recognised" % encoding
73 def decode_elwidth(encoding
):
74 pmap
= {'8': 0b11, '16': 0b10, '32': 0b01}
75 assert encoding
in pmap
, \
76 "encoding %s for elwidth not recognised" % encoding
80 # decodes predicate register encoding
81 def decode_predicate(encoding
):
92 'nl' : (1, 0b001), 'ge' : (1, 0b001), # same value
94 'ng' : (1, 0b011), 'le' : (1, 0b011), # same value
97 'so' : (1, 0b110), 'un' : (1, 0b110), # same value
98 'ns' : (1, 0b111), 'nu' : (1, 0b111), # same value
100 assert encoding
in pmap
, \
101 "encoding %s for predicate not recognised" % encoding
102 return pmap
[encoding
]
105 # decodes "Mode" in similar way to BO field (supposed to, anyway)
106 def decode_bo(encoding
):
107 pmap
= { # TODO: double-check that these are the same as Branch BO
109 'nl' : 0b001, 'ge' : 0b001, # same value
111 'ng' : 0b011, 'le' : 0b011, # same value
114 'so' : 0b110, 'un' : 0b110, # same value
115 'ns' : 0b111, 'nu' : 0b111, # same value
117 assert encoding
in pmap
, \
118 "encoding %s for BO Mode not recognised" % encoding
119 return pmap
[encoding
]
121 # partial-decode fail-first mode
122 def decode_ffirst(encoding
):
123 if encoding
in ['RC1', '~RC1']:
125 return decode_bo(encoding
)
128 def decode_reg(field
):
129 # decode the field number. "5.v" or "3.s" or "9"
130 field
= field
.split(".")
131 regmode
= 'scalar' # default
135 elif field
[1] == 'v':
137 field
= int(field
[0]) # actual register number
138 return field
, regmode
141 def decode_imm(field
):
142 ldst_imm
= "(" in field
and field
[-1] == ')'
144 return field
[:-1].split("(")
149 # decodes svp64 assembly listings and creates EXT001 svp64 prefixes
151 def __init__(self
, lst
, bigendian
=False, macros
=None):
156 self
.trans
= self
.translate(lst
)
157 self
.isa
= ISA() # reads the v3.0B pseudo-code markdown files
158 self
.svp64
= SVP64RM() # reads the svp64 Remap entries for registers
159 assert bigendian
== False, "error, bigendian not supported yet"
162 yield from self
.trans
164 def translate_one(self
, insn
, macros
=None):
167 macros
.update(self
.macros
)
170 # find first space, to get opcode
173 # now find opcode fields
174 fields
= ''.join(ls
[1:]).split(',')
175 mfields
= list(map(str.strip
, fields
))
176 log ("opcode, fields", ls
, opcode
, mfields
)
179 for field
in mfields
:
180 fields
.append(macro_subst(macros
, field
))
181 log ("opcode, fields substed", ls
, opcode
, fields
)
183 # sigh have to do setvl here manually for now...
184 # note the subtract one from SVi.
185 if opcode
in ["setvl", "setvl."]:
186 insn
= 22 << (31-5) # opcode 22, bits 0-5
187 fields
= list(map(int, fields
))
188 insn |
= fields
[0] << (31-10) # RT , bits 6-10
189 insn |
= fields
[1] << (31-15) # RA , bits 11-15
190 insn |
= (fields
[2]-1) << (31-22) # SVi , bits 16-22
191 insn |
= fields
[3] << (31-25) # ms , bit 25
192 insn |
= fields
[4] << (31-24) # vs , bit 24
193 insn |
= fields
[5] << (31-23) # vf , bit 23
194 insn |
= 0b00000 << (31-30) # XO , bits 26..30
195 if opcode
== 'setvl.':
196 insn |
= 1 << (31-31) # Rc=1 , bit 31
197 log ("setvl", bin(insn
))
198 yield ".long 0x%x" % insn
201 # sigh have to do setvl here manually for now...
202 # note the subtract one from SVi.
203 if opcode
in ["svstep", "svstep."]:
204 insn
= 22 << (31-5) # opcode 22, bits 0-5
205 fields
= list(map(int, fields
))
206 insn |
= fields
[0] << (31-10) # RT , bits 6-10
207 insn |
= (fields
[1]-1) << (31-22) # SVi , bits 16-22
208 insn |
= fields
[2] << (31-25) # vf , bit 25
209 insn |
= 0b00011 << (31-30) # XO , bits 26..30
210 if opcode
== 'svstep.':
211 insn |
= 1 << (31-31) # Rc=1 , bit 31
212 log ("svstep", bin(insn
))
213 yield ".long 0x%x" % insn
216 # and svshape. note that the dimension fields one subtracted from each
217 if opcode
== 'svshape':
218 insn
= 22 << (31-5) # opcode 22, bits 0-5
219 fields
= list(map(int, fields
))
220 insn |
= (fields
[0]-1) << (31-10) # SVxd , bits 6-10
221 insn |
= (fields
[1]-1) << (31-15) # SVyd , bits 11-15
222 insn |
= (fields
[2]-1) << (31-20) # SVzd , bits 16-20
223 insn |
= (fields
[3]) << (31-24) # SVRM , bits 21-24
224 insn |
= (fields
[4]) << (31-25) # vf , bits 25
225 insn |
= 0b00001 << (31-30) # XO , bits 26..30
227 log ("svshape", bin(insn
))
228 yield ".long 0x%x" % insn
232 if opcode
== 'svremap':
233 insn
= 22 << (31-5) # opcode 22, bits 0-5
234 fields
= list(map(int, fields
))
235 insn |
= fields
[0] << (31-10) # SVme , bits 6-10
236 insn |
= fields
[1] << (31-12) # mi0 , bits 11-12
237 insn |
= fields
[2] << (31-14) # mi1 , bits 13-14
238 insn |
= fields
[3] << (31-16) # mi2 , bits 15-16
239 insn |
= fields
[4] << (31-18) # m00 , bits 17-18
240 insn |
= fields
[5] << (31-20) # m01 , bits 19-20
241 insn |
= fields
[6] << (31-21) # m01 , bit 21
242 insn |
= 0b00010 << (31-30) # XO , bits 26..30
244 log ("svremap", bin(insn
))
245 yield ".long 0x%x" % insn
249 # XXX WARNING THESE ARE NOT APPROVED BY OPF ISA WG
250 # however we are out of space with opcode 22
251 if opcode
.startswith('fsins'):
252 fields
= list(map(int, fields
))
253 insn
= 59 << (31-5) # opcode 59, bits 0-5
254 insn |
= fields
[0] << (31-10) # RT , bits 6-10
255 insn |
= fields
[1] << (31-20) # RB , bits 16-20
256 insn |
= 0b1000001110 << (31-30) # XO , bits 21..30
257 if opcode
== 'fsins.':
258 insn |
= 1 << (31-31) # Rc=1 , bit 31
259 log ("fsins", bin(insn
))
260 yield ".long 0x%x" % insn
264 # XXX WARNING THESE ARE NOT APPROVED BY OPF ISA WG
265 # however we are out of space with opcode 22
266 if opcode
.startswith('fcoss'):
267 fields
= list(map(int, fields
))
268 insn
= 59 << (31-5) # opcode 59, bits 0-5
269 insn |
= fields
[0] << (31-10) # RT , bits 6-10
270 insn |
= fields
[1] << (31-20) # RB , bits 16-20
271 insn |
= 0b1000101110 << (31-30) # XO , bits 21..30
272 if opcode
== 'fcoss.':
273 insn |
= 1 << (31-31) # Rc=1 , bit 31
274 log ("fcoss", bin(insn
))
275 yield ".long 0x%x" % insn
278 # identify if is a svp64 mnemonic
279 if not opcode
.startswith('sv.'):
280 yield insn
# unaltered
282 opcode
= opcode
[3:] # strip leading "sv"
284 # start working on decoding the svp64 op: sv.basev30Bop/vec2/mode
285 opmodes
= opcode
.split("/") # split at "/"
286 v30b_op
= opmodes
.pop(0) # first is the v3.0B
287 # check instruction ends with dot
288 rc_mode
= v30b_op
.endswith('.')
290 v30b_op
= v30b_op
[:-1]
292 # sigh again, have to recognised LD/ST bit-reverse instructions
293 # this has to be "processed" to fit into a v3.0B without the "sh"
294 # e.g. ldsh is actually ld
295 ldst_shift
= v30b_op
.startswith("l") and v30b_op
.endswith("sh")
297 if v30b_op
not in isa
.instr
:
298 raise Exception("opcode %s of '%s' not supported" % \
302 # okaay we need to process the fields and make this:
303 # ldsh RT, SVD(RA), RC - 11 bits for SVD, 5 for RC
305 # ld RT, D(RA) - 16 bits
306 # likewise same for SVDS (9 bits for SVDS, 5 for RC, 14 bits for DS)
307 form
= isa
.instr
[v30b_op
].form
# get form (SVD-Form, SVDS-Form)
311 # identify if this is a ld/st immediate(reg) thing
312 ldst_imm
= "(" in field
and field
[-1] == ')'
314 newfields
.append(field
[:-1].split("("))
316 newfields
.append(field
)
318 immed
, RA
= newfields
[1]
320 RC
= int(newfields
.pop(2)) # better be an integer number!
321 if form
== 'SVD': # 16 bit: immed 11 bits, RC shift up 11
322 immed
= (immed
& 0b11111111111) |
(RC
<<11)
323 if immed
& (1<<15): # should be negative
325 if form
== 'SVDS': # 14 bit: immed 9 bits, RC shift up 9
326 immed
= (immed
& 0b111111111) |
(RC
<<9)
327 if immed
& (1<<13): # should be negative
329 newfields
[1] = "%d(%s)" % (immed
, RA
)
332 # and strip off "sh" from end, and add "sh" to opmodes, instead
333 v30b_op
= v30b_op
[:-2]
335 log ("rewritten", v30b_op
, opmodes
, fields
)
337 if v30b_op
not in svp64
.instrs
:
338 raise Exception("opcode %s of '%s' not an svp64 instruction" % \
340 v30b_regs
= isa
.instr
[v30b_op
].regs
[0] # get regs info "RT, RA, RB"
341 rm
= svp64
.instrs
[v30b_op
] # one row of the svp64 RM CSV
342 log ("v3.0B op", v30b_op
, "Rc=1" if rc_mode
else '')
343 log ("v3.0B regs", opcode
, v30b_regs
)
346 # right. the first thing to do is identify the ordering of
347 # the registers, by name. the EXTRA2/3 ordering is in
348 # rm['0']..rm['3'] but those fields contain the names RA, BB
349 # etc. we have to read the pseudocode to understand which
350 # reg is which in our instruction. sigh.
352 # first turn the svp64 rm into a "by name" dict, recording
353 # which position in the RM EXTRA it goes into
354 # also: record if the src or dest was a CR, for sanity-checking
355 # (elwidth overrides on CRs are banned)
356 decode
= decode_extra(rm
)
357 dest_reg_cr
, src_reg_cr
, svp64_src
, svp64_dest
= decode
359 log ("EXTRA field index, src", svp64_src
)
360 log ("EXTRA field index, dest", svp64_dest
)
362 # okaaay now we identify the field value (opcode N,N,N) with
363 # the pseudo-code info (opcode RT, RA, RB)
364 assert len(fields
) == len(v30b_regs
), \
365 "length of fields %s must match insn `%s` fields %s" % \
366 (str(v30b_regs
), insn
, str(fields
))
367 opregfields
= zip(fields
, v30b_regs
) # err that was easy
369 # now for each of those find its place in the EXTRA encoding
370 # note there is the possibility (for LD/ST-with-update) of
371 # RA occurring **TWICE**. to avoid it getting added to the
372 # v3.0B suffix twice, we spot it as a duplicate, here
373 extras
= OrderedDict()
374 for idx
, (field
, regname
) in enumerate(opregfields
):
375 imm
, regname
= decode_imm(regname
)
376 rtype
= get_regtype(regname
)
377 log (" idx find", rtype
, idx
, field
, regname
, imm
)
379 # probably an immediate field, append it straight
380 extras
[('imm', idx
, False)] = (idx
, field
, None, None, None)
382 extra
= svp64_src
.get(regname
, None)
383 if extra
is not None:
384 extra
= ('s', extra
, False) # not a duplicate
385 extras
[extra
] = (idx
, field
, regname
, rtype
, imm
)
386 log (" idx src", idx
, extra
, extras
[extra
])
387 dextra
= svp64_dest
.get(regname
, None)
388 log ("regname in", regname
, dextra
)
389 if dextra
is not None:
390 is_a_duplicate
= extra
is not None # duplicate spotted
391 dextra
= ('d', dextra
, is_a_duplicate
)
392 extras
[dextra
] = (idx
, field
, regname
, rtype
, imm
)
393 log (" idx dst", idx
, extra
, extras
[dextra
])
395 # great! got the extra fields in their associated positions:
396 # also we know the register type. now to create the EXTRA encodings
397 etype
= rm
['Etype'] # Extra type: EXTRA3/EXTRA2
398 ptype
= rm
['Ptype'] # Predication type: Twin / Single
401 for extra_idx
, (idx
, field
, rname
, rtype
, iname
) in extras
.items():
402 # is it a field we don't alter/examine? if so just put it
405 v30b_newfields
.append(field
)
408 # identify if this is a ld/st immediate(reg) thing
409 ldst_imm
= "(" in field
and field
[-1] == ')'
411 immed
, field
= field
[:-1].split("(")
413 field
, regmode
= decode_reg(field
)
414 log (" ", extra_idx
, rname
, rtype
,
415 regmode
, iname
, field
, end
=" ")
417 # see Mode field https://libre-soc.org/openpower/sv/svp64/
418 # XXX TODO: the following is a bit of a laborious repeated
419 # mess, which could (and should) easily be parameterised.
420 # XXX also TODO: the LD/ST modes which are different
421 # https://libre-soc.org/openpower/sv/ldst/
423 # encode SV-GPR and SV-FPR field into extra, v3.0field
424 if rtype
in ['GPR', 'FPR']:
425 sv_extra
, field
= get_extra_gpr(etype
, regmode
, field
)
426 # now sanity-check. EXTRA3 is ok, EXTRA2 has limits
427 # (and shrink to a single bit if ok)
428 if etype
== 'EXTRA2':
429 if regmode
== 'scalar':
430 # range is r0-r63 in increments of 1
431 assert (sv_extra
>> 1) == 0, \
432 "scalar GPR %s cannot fit into EXTRA2 %s" % \
433 (rname
, str(extras
[extra_idx
]))
434 # all good: encode as scalar
435 sv_extra
= sv_extra
& 0b01
437 # range is r0-r127 in increments of 4
438 assert sv_extra
& 0b01 == 0, \
439 "%s: vector field %s cannot fit " \
441 (insn
, rname
, str(extras
[extra_idx
]))
442 # all good: encode as vector (bit 2 set)
443 sv_extra
= 0b10 |
(sv_extra
>> 1)
444 elif regmode
== 'vector':
445 # EXTRA3 vector bit needs marking
448 # encode SV-CR 3-bit field into extra, v3.0field
449 elif rtype
== 'CR_3bit':
450 sv_extra
, field
= get_extra_cr_3bit(etype
, regmode
, field
)
451 # now sanity-check (and shrink afterwards)
452 if etype
== 'EXTRA2':
453 if regmode
== 'scalar':
454 # range is CR0-CR15 in increments of 1
455 assert (sv_extra
>> 1) == 0, \
456 "scalar CR %s cannot fit into EXTRA2 %s" % \
457 (rname
, str(extras
[extra_idx
]))
458 # all good: encode as scalar
459 sv_extra
= sv_extra
& 0b01
461 # range is CR0-CR127 in increments of 16
462 assert sv_extra
& 0b111 == 0, \
463 "vector CR %s cannot fit into EXTRA2 %s" % \
464 (rname
, str(extras
[extra_idx
]))
465 # all good: encode as vector (bit 2 set)
466 sv_extra
= 0b10 |
(sv_extra
>> 3)
468 if regmode
== 'scalar':
469 # range is CR0-CR31 in increments of 1
470 assert (sv_extra
>> 2) == 0, \
471 "scalar CR %s cannot fit into EXTRA2 %s" % \
472 (rname
, str(extras
[extra_idx
]))
473 # all good: encode as scalar
474 sv_extra
= sv_extra
& 0b11
476 # range is CR0-CR127 in increments of 8
477 assert sv_extra
& 0b11 == 0, \
478 "vector CR %s cannot fit into EXTRA2 %s" % \
479 (rname
, str(extras
[extra_idx
]))
480 # all good: encode as vector (bit 3 set)
481 sv_extra
= 0b100 |
(sv_extra
>> 2)
483 # encode SV-CR 5-bit field into extra, v3.0field
484 # *sigh* this is the same as 3-bit except the 2 LSBs are
486 elif rtype
== 'CR_5bit':
487 cr_subfield
= field
& 0b11
488 field
= field
>> 2 # strip bottom 2 bits
489 sv_extra
, field
= get_extra_cr_3bit(etype
, regmode
, field
)
490 # now sanity-check (and shrink afterwards)
491 if etype
== 'EXTRA2':
492 if regmode
== 'scalar':
493 # range is CR0-CR15 in increments of 1
494 assert (sv_extra
>> 1) == 0, \
495 "scalar CR %s cannot fit into EXTRA2 %s" % \
496 (rname
, str(extras
[extra_idx
]))
497 # all good: encode as scalar
498 sv_extra
= sv_extra
& 0b01
500 # range is CR0-CR127 in increments of 16
501 assert sv_extra
& 0b111 == 0, \
502 "vector CR %s cannot fit into EXTRA2 %s" % \
503 (rname
, str(extras
[extra_idx
]))
504 # all good: encode as vector (bit 2 set)
505 sv_extra
= 0b10 |
(sv_extra
>> 3)
507 if regmode
== 'scalar':
508 # range is CR0-CR31 in increments of 1
509 assert (sv_extra
>> 2) == 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
& 0b11
515 # range is CR0-CR127 in increments of 8
516 assert sv_extra
& 0b11 == 0, \
517 "vector CR %s cannot fit into EXTRA3 %s" % \
518 (rname
, str(extras
[extra_idx
]))
519 # all good: encode as vector (bit 3 set)
520 sv_extra
= 0b100 |
(sv_extra
>> 2)
521 # reconstruct the actual 5-bit CR field
522 field
= (field
<< 2) | cr_subfield
525 print ("no type match", rtype
)
527 # capture the extra field info
528 log ("=>", "%5s" % bin(sv_extra
), field
)
529 extras
[extra_idx
] = sv_extra
531 # append altered field value to v3.0b, differs for LDST
532 # note that duplicates are skipped e.g. EXTRA2 contains
533 # *BOTH* s:RA *AND* d:RA which happens on LD/ST-with-update
534 srcdest
, idx
, duplicate
= extra_idx
535 if duplicate
: # skip adding to v3.0b fields, already added
538 v30b_newfields
.append(("%s(%s)" % (immed
, str(field
))))
540 v30b_newfields
.append(str(field
))
542 log ("new v3.0B fields", v30b_op
, v30b_newfields
)
543 log ("extras", extras
)
545 # rright. now we have all the info. start creating SVP64 RM
546 svp64_rm
= SVP64RMFields()
548 # begin with EXTRA fields
549 for idx
, sv_extra
in extras
.items():
551 if idx
is None: continue
552 if idx
[0] == 'imm': continue
553 srcdest
, idx
, duplicate
= idx
554 if etype
== 'EXTRA2':
555 svp64_rm
.extra2
[idx
].eq(
556 SelectableInt(sv_extra
, SVP64RM_EXTRA2_SPEC_SIZE
))
558 svp64_rm
.extra3
[idx
].eq(
559 SelectableInt(sv_extra
, SVP64RM_EXTRA3_SPEC_SIZE
))
561 # identify if the op is a LD/ST. the "blegh" way. copied
562 # from power_enums. TODO, split the list _insns down.
564 "lbarx", "lbz", "lbzu", "lbzux", "lbzx", # load byte
565 "ld", "ldarx", "ldbrx", "ldu", "ldux", "ldx", # load double
566 "lfs", "lfsx", "lfsu", "lfsux", # FP load single
567 "lfd", "lfdx", "lfdu", "lfdux", "lfiwzx", "lfiwax", # FP load double
568 "lha", "lharx", "lhau", "lhaux", "lhax", # load half
569 "lhbrx", "lhz", "lhzu", "lhzux", "lhzx", # more load half
570 "lwa", "lwarx", "lwaux", "lwax", "lwbrx", # load word
571 "lwz", "lwzcix", "lwzu", "lwzux", "lwzx", # more load word
574 "stb", "stbcix", "stbcx", "stbu", "stbux", "stbx",
575 "std", "stdbrx", "stdcx", "stdu", "stdux", "stdx",
576 "stfs", "stfsx", "stfsu", "stfux", # FP store single
577 "stfd", "stfdx", "stfdu", "stfdux", "stfiwx", # FP store double
578 "sth", "sthbrx", "sthcx", "sthu", "sthux", "sthx",
579 "stw", "stwbrx", "stwcx", "stwu", "stwux", "stwx",
581 # use this to determine if the SVP64 RM format is different.
582 # see https://libre-soc.org/openpower/sv/ldst/
583 is_ldst
= is_ld
or is_st
585 # branch-conditional detection
593 destwid
= 0 # bits 4-5
594 srcwid
= 0 # bits 6-7
596 smask
= 0 # bits 16-18 but only for twin-predication
597 mode
= 0 # bits 19-23
599 mask_m_specified
= False
610 mapreduce_crm
= False
611 mapreduce_svm
= False
617 # branch-conditional bits
627 # ok let's start identifying opcode augmentation fields
628 for encmode
in opmodes
:
629 # predicate mask (src and dest)
630 if encmode
.startswith("m="):
632 pmmode
, pmask
= decode_predicate(encmode
[2:])
633 smmode
, smask
= pmmode
, pmask
635 mask_m_specified
= True
636 # predicate mask (dest)
637 elif encmode
.startswith("dm="):
639 pmmode
, pmask
= decode_predicate(encmode
[3:])
642 # predicate mask (src, twin-pred)
643 elif encmode
.startswith("sm="):
645 smmode
, smask
= decode_predicate(encmode
[3:])
649 elif encmode
.startswith("sh"):
652 elif encmode
.startswith("vec"):
653 subvl
= decode_subvl(encmode
[3:])
655 elif encmode
.startswith("ew="):
656 destwid
= decode_elwidth(encmode
[3:])
657 elif encmode
.startswith("sw="):
658 srcwid
= decode_elwidth(encmode
[3:])
659 # element-strided LD/ST
660 elif encmode
== 'els':
663 elif encmode
== 'sats':
664 assert sv_mode
is None
667 elif encmode
== 'satu':
668 assert sv_mode
is None
672 elif encmode
== 'sz':
674 elif encmode
== 'dz':
677 elif encmode
.startswith("ff="):
678 assert sv_mode
is None
680 failfirst
= decode_ffirst(encmode
[3:])
681 # predicate-result, interestingly same as fail-first
682 elif encmode
.startswith("pr="):
683 assert sv_mode
is None
685 predresult
= decode_ffirst(encmode
[3:])
686 # map-reduce mode, reverse-gear
687 elif encmode
== 'mrr':
688 assert sv_mode
is None
693 elif encmode
== 'mr':
694 assert sv_mode
is None
697 elif encmode
== 'crm': # CR on map-reduce
698 assert sv_mode
is None
701 elif encmode
== 'svm': # sub-vector mode
706 elif encmode
== 'st': # svstep mode
708 elif encmode
== 'sr': # svstep BRc mode
711 elif encmode
== 'vs': # VLSET mode
713 elif encmode
== 'vsi': # VLSET mode with VLI (VL inclusives)
716 elif encmode
== 'vsb': # VLSET mode with VSb
719 elif encmode
== 'vsbi': # VLSET mode with VLI and VSb
723 elif encmode
== 'snz': # sz (only) already set above
726 elif encmode
== 'lu': # LR update mode
729 raise AssertionError("unknown encmode %s" % encmode
)
731 raise AssertionError("unknown encmode %s" % encmode
)
734 # since m=xx takes precedence (overrides) sm=xx and dm=xx,
735 # treat them as mutually exclusive
737 assert not has_smask
,\
738 "cannot have both source-mask and predicate mask"
739 assert not has_pmask
,\
740 "cannot have both dest-mask and predicate mask"
741 # since the default is INT predication (ALWAYS), if you
742 # specify one CR mask, you must specify both, to avoid
743 # mixing INT and CR reg types
744 if has_pmask
and pmmode
== 1:
746 "need explicit source-mask in CR twin predication"
747 if has_smask
and smmode
== 1:
749 "need explicit dest-mask in CR twin predication"
750 # sanity-check that 2Pred mask is same mode
751 if has_pmask
and has_smask
:
752 assert smmode
== pmmode
, \
753 "predicate masks %s and %s must be same reg type" % \
756 # sanity-check that twin-predication mask only specified in 2P mode
758 assert not has_smask
, \
759 "source-mask can only be specified on Twin-predicate ops"
760 assert not has_pmask
, \
761 "dest-mask can only be specified on Twin-predicate ops"
763 # construct the mode field, doing sanity-checking along the way
765 assert sv_mode
== 0b00, "sub-vector mode in mapreduce only"
766 assert subvl
!= 0, "sub-vector mode not possible on SUBVL=1"
769 assert has_smask
or mask_m_specified
, \
770 "src zeroing requires a source predicate"
772 assert has_pmask
or mask_m_specified
, \
773 "dest zeroing requires a dest predicate"
775 # check LDST shifted, only available in "normal" mode
776 if is_ldst
and ldst_shift
:
777 assert sv_mode
is None, \
778 "LD shift cannot have modes (%s) applied" % sv_mode
780 # now create mode and (overridden) src/dst widths
781 # XXX TODO: sanity-check bc modes
783 sv_mode
= ((bc_svstep
<< SVP64MODE
.MOD2_MSB
) |
784 (bc_vlset
<< SVP64MODE
.MOD2_LSB
) |
785 (bc_snz
<< SVP64MODE
.BC_SNZ
))
786 srcwid
= (bc_vsb
<< 1) | bc_lru
787 destwid
= (bc_lru
<< 1) | bc_all
791 ######################################
794 mode |
= src_zero
<< SVP64MODE
.SZ
# predicate zeroing
795 mode |
= dst_zero
<< SVP64MODE
.DZ
# predicate zeroing
797 # TODO: for now, LD/ST-indexed is ignored.
798 mode |
= ldst_elstride
<< SVP64MODE
.ELS_NORMAL
# el-strided
801 mode |
= 1 << SVP64MODE
.LDST_SHIFT
803 # TODO, reduce and subvector mode
804 # 00 1 dz CRM reduce mode (mapreduce), SUBVL=1
805 # 00 1 SVM CRM subvector reduce mode, SUBVL>1
809 ######################################
811 elif sv_mode
== 0b00:
812 mode |
= (0b1<<SVP64MODE
.REDUCE
) # sets mapreduce
813 assert dst_zero
== 0, "dest-zero not allowed in mapreduce mode"
815 mode |
= (0b1<<SVP64MODE
.RG
) # sets Reverse-gear mode
817 mode |
= (0b1<<SVP64MODE
.CRM
) # sets CRM mode
818 assert rc_mode
, "CRM only allowed when Rc=1"
819 # bit of weird encoding to jam zero-pred or SVM mode in.
820 # SVM mode can be enabled only when SUBVL=2/3/4 (vec2/3/4)
822 mode |
= dst_zero
<< SVP64MODE
.DZ
# predicate zeroing
824 mode |
= (0b1<<SVP64MODE
.SVM
) # sets SVM mode
826 ######################################
828 elif sv_mode
== 0b01:
829 assert src_zero
== 0, "dest-zero not allowed in failfirst mode"
830 if failfirst
== 'RC1':
831 mode |
= (0b1<<SVP64MODE
.RC1
) # sets RC1 mode
832 mode |
= (dst_zero
<< SVP64MODE
.DZ
) # predicate dst-zeroing
833 assert rc_mode
==False, "ffirst RC1 only possible when Rc=0"
834 elif failfirst
== '~RC1':
835 mode |
= (0b1<<SVP64MODE
.RC1
) # sets RC1 mode
836 mode |
= (dst_zero
<< SVP64MODE
.DZ
) # predicate dst-zeroing
837 mode |
= (0b1<<SVP64MODE
.INV
) # ... with inversion
838 assert rc_mode
==False, "ffirst RC1 only possible when Rc=0"
840 assert dst_zero
== 0, "dst-zero not allowed in ffirst BO"
841 assert rc_mode
, "ffirst BO only possible when Rc=1"
842 mode |
= (failfirst
<< SVP64MODE
.BO_LSB
) # set BO
844 ######################################
846 elif sv_mode
== 0b10:
847 mode |
= src_zero
<< SVP64MODE
.SZ
# predicate zeroing
848 mode |
= dst_zero
<< SVP64MODE
.DZ
# predicate zeroing
849 mode |
= (saturation
<< SVP64MODE
.N
) # signed/us saturation
851 ######################################
852 # "predicate-result" modes. err... code-duplication from ffirst
853 elif sv_mode
== 0b11:
854 assert src_zero
== 0, "dest-zero not allowed in predresult mode"
855 if predresult
== 'RC1':
856 mode |
= (0b1<<SVP64MODE
.RC1
) # sets RC1 mode
857 mode |
= (dst_zero
<< SVP64MODE
.DZ
) # predicate dst-zeroing
858 assert rc_mode
==False, "pr-mode RC1 only possible when Rc=0"
859 elif predresult
== '~RC1':
860 mode |
= (0b1<<SVP64MODE
.RC1
) # sets RC1 mode
861 mode |
= (dst_zero
<< SVP64MODE
.DZ
) # predicate dst-zeroing
862 mode |
= (0b1<<SVP64MODE
.INV
) # ... with inversion
863 assert rc_mode
==False, "pr-mode RC1 only possible when Rc=0"
865 assert dst_zero
== 0, "dst-zero not allowed in pr-mode BO"
866 assert rc_mode
, "pr-mode BO only possible when Rc=1"
867 mode |
= (predresult
<< SVP64MODE
.BO_LSB
) # set BO
869 # whewww.... modes all done :)
870 # now put into svp64_rm
873 svp64_rm
.mode
.eq(SelectableInt(mode
, SVP64RM_MODE_SIZE
))
875 # put in predicate masks into svp64_rm
877 # source pred: bits 16-18
878 svp64_rm
.smask
.eq(SelectableInt(smask
, SVP64RM_SMASK_SIZE
))
880 svp64_rm
.mmode
.eq(SelectableInt(mmode
, SVP64RM_MMODE_SIZE
))
882 svp64_rm
.mask
.eq(SelectableInt(pmask
, SVP64RM_MASK_SIZE
))
884 # and subvl: bits 8-9
885 svp64_rm
.subvl
.eq(SelectableInt(subvl
, SVP64RM_SUBVL_SIZE
))
889 svp64_rm
.ewsrc
.eq(SelectableInt(srcwid
, SVP64RM_EWSRC_SIZE
))
891 svp64_rm
.elwidth
.eq(SelectableInt(destwid
, SVP64RM_ELWIDTH_SIZE
))
893 # nice debug printout. (and now for something completely different)
894 # https://youtu.be/u0WOIwlXE9g?t=146
895 svp64_rm_value
= svp64_rm
.spr
.value
896 log ("svp64_rm", hex(svp64_rm_value
), bin(svp64_rm_value
))
897 log (" mmode 0 :", bin(mmode
))
898 log (" pmask 1-3 :", bin(pmask
))
899 log (" dstwid 4-5 :", bin(destwid
))
900 log (" srcwid 6-7 :", bin(srcwid
))
901 log (" subvl 8-9 :", bin(subvl
))
902 log (" mode 19-23:", bin(mode
))
903 offs
= 2 if etype
== 'EXTRA2' else 3 # 2 or 3 bits
904 for idx
, sv_extra
in extras
.items():
905 if idx
is None: continue
906 if idx
[0] == 'imm': continue
907 srcdest
, idx
, duplicate
= idx
908 start
= (10+idx
*offs
)
910 log (" extra%d %2d-%2d:" % (idx
, start
, end
),
913 log (" smask 16-17:", bin(smask
))
916 # first, construct the prefix from its subfields
917 svp64_prefix
= SVP64PrefixFields()
918 svp64_prefix
.major
.eq(SelectableInt(0x1, SV64P_MAJOR_SIZE
))
919 svp64_prefix
.pid
.eq(SelectableInt(0b11, SV64P_PID_SIZE
))
920 svp64_prefix
.rm
.eq(svp64_rm
.spr
)
922 # fiinally yield the svp64 prefix and the thingy. v3.0b opcode
923 rc
= '.' if rc_mode
else ''
924 yield ".long 0x%x" % svp64_prefix
.insn
.value
926 # argh, sv.fmadds etc. need to be done manually
927 if v30b_op
== 'ffmadds':
928 opcode
= 59 << (32-6) # bits 0..6 (MSB0)
929 opcode |
= int(v30b_newfields
[0]) << (32-11) # FRT
930 opcode |
= int(v30b_newfields
[1]) << (32-16) # FRA
931 opcode |
= int(v30b_newfields
[2]) << (32-21) # FRB
932 opcode |
= int(v30b_newfields
[3]) << (32-26) # FRC
933 opcode |
= 0b00101 << (32-31) # bits 26-30
935 opcode |
= 1 # Rc, bit 31.
936 yield ".long 0x%x" % opcode
937 # argh, sv.fdmadds need to be done manually
938 elif v30b_op
== 'fdmadds':
939 opcode
= 59 << (32-6) # bits 0..6 (MSB0)
940 opcode |
= int(v30b_newfields
[0]) << (32-11) # FRT
941 opcode |
= int(v30b_newfields
[1]) << (32-16) # FRA
942 opcode |
= int(v30b_newfields
[2]) << (32-21) # FRB
943 opcode |
= int(v30b_newfields
[3]) << (32-26) # FRC
944 opcode |
= 0b01111 << (32-31) # bits 26-30
946 opcode |
= 1 # Rc, bit 31.
947 yield ".long 0x%x" % opcode
948 # argh, sv.ffadds etc. need to be done manually
949 elif v30b_op
== 'ffadds':
950 opcode
= 59 << (32-6) # bits 0..6 (MSB0)
951 opcode |
= int(v30b_newfields
[0]) << (32-11) # FRT
952 opcode |
= int(v30b_newfields
[1]) << (32-16) # FRA
953 opcode |
= int(v30b_newfields
[2]) << (32-21) # FRB
954 opcode |
= 0b01101 << (32-31) # bits 26-30
956 opcode |
= 1 # Rc, bit 31.
957 yield ".long 0x%x" % opcode
958 # sigh have to do svstep here manually for now...
959 elif opcode
in ["svstep", "svstep."]:
960 insn
= 22 << (31-5) # opcode 22, bits 0-5
961 insn |
= int(v30b_newfields
[0]) << (31-10) # RT , bits 6-10
962 insn |
= int(v30b_newfields
[1]) << (31-22) # SVi , bits 16-22
963 insn |
= int(v30b_newfields
[2]) << (31-25) # vf , bit 25
964 insn |
= 0b00011 << (31-30) # XO , bits 26..30
965 if opcode
== 'svstep.':
966 insn |
= 1 << (31-31) # Rc=1 , bit 31
967 log ("svstep", bin(insn
))
968 yield ".long 0x%x" % insn
970 elif v30b_op
in ["setvl", "setvl."]:
971 insn
= 22 << (31-5) # opcode 22, bits 0-5
972 fields
= list(map(int, fields
))
973 insn |
= fields
[0] << (31-10) # RT , bits 6-10
974 insn |
= fields
[1] << (31-15) # RA , bits 11-15
975 insn |
= (fields
[2]-1) << (31-22) # SVi , bits 16-22
976 insn |
= fields
[3] << (31-25) # ms , bit 25
977 insn |
= fields
[4] << (31-24) # vs , bit 24
978 insn |
= fields
[5] << (31-23) # vf , bit 23
979 insn |
= 0b00000 << (31-30) # XO , bits 26..30
980 if opcode
== 'setvl.':
981 insn |
= 1 << (31-31) # Rc=1 , bit 31
982 log ("setvl", bin(insn
))
983 yield ".long 0x%x" % insn
985 elif v30b_op
in ["fcoss", "fcoss."]:
986 insn
= 59 << (31-5) # opcode 59, bits 0-5
987 insn |
= int(v30b_newfields
[0]) << (31-10) # RT , bits 6-10
988 insn |
= int(v30b_newfields
[1]) << (31-20) # RB , bits 16-20
989 insn |
= 0b1000101110 << (31-30) # XO , bits 21..30
990 if opcode
== 'fcoss.':
991 insn |
= 1 << (31-31) # Rc=1 , bit 31
992 log ("fcoss", bin(insn
))
993 yield ".long 0x%x" % insn
996 yield "%s %s" % (v30b_op
+rc
, ", ".join(v30b_newfields
))
997 log ("new v3.0B fields", v30b_op
, v30b_newfields
)
999 def translate(self
, lst
):
1001 yield from self
.translate_one(insn
)
1004 def macro_subst(macros
, txt
):
1006 print ("subst", txt
, macros
)
1009 for macro
, value
in macros
.items():
1012 replaced
= txt
.replace(macro
, value
)
1013 print ("macro", txt
, "replaced", replaced
, macro
, value
)
1016 toreplace
= '%s.s' % macro
1017 if toreplace
== txt
:
1019 replaced
= txt
.replace(toreplace
, "%s.s" % value
)
1020 print ("macro", txt
, "replaced", replaced
, toreplace
, value
)
1023 toreplace
= '%s.v' % macro
1024 if toreplace
== txt
:
1026 replaced
= txt
.replace(toreplace
, "%s.v" % value
)
1027 print ("macro", txt
, "replaced", replaced
, toreplace
, value
)
1030 toreplace
= '(%s)' % macro
1031 if toreplace
in txt
:
1033 replaced
= txt
.replace(toreplace
, '(%s)' % value
)
1034 print ("macro", txt
, "replaced", replaced
, toreplace
, value
)
1037 print (" processed", txt
)
1045 if not line
[0].isspace():
1054 # get an input file and an output file
1058 outfile
= sys
.stdout
1059 # read the whole lot in advance in case of in-place
1060 lines
= list(infile
.readlines())
1061 elif len(args
) != 2:
1062 print ("pysvp64asm [infile | -] [outfile | -]")
1068 infile
= open(args
[0], "r")
1069 # read the whole lot in advance in case of in-place overwrite
1070 lines
= list(infile
.readlines())
1073 outfile
= sys
.stdout
1075 outfile
= open(args
[1], "w")
1077 # read the line, look for "sv", process it
1078 macros
= {} # macros which start ".set"
1081 ls
= line
.split("#")
1084 if op
.startswith("setvl") or op
.startswith("svshape"):
1085 ws
, line
= get_ws(ls
[0])
1086 lst
= list(isa
.translate_one(ls
[0].strip(), macros
))
1087 lst
= '; '.join(lst
)
1088 outfile
.write("%s%s # %s\n" % (ws
, lst
, ls
[0]))
1090 if ls
[0].startswith(".set"):
1091 macro
= ls
[0][4:].split(",")
1092 macro
, value
= list(map(str.strip
, macro
))
1093 macros
[macro
] = value
1097 potential
= ls
[1].strip()
1098 if not potential
.startswith("sv."):
1102 ws
, line
= get_ws(line
)
1103 # SV line indentified
1104 lst
= list(isa
.translate_one(potential
, macros
))
1105 lst
= '; '.join(lst
)
1106 outfile
.write("%s%s # %s\n" % (ws
, lst
, potential
))
1109 if __name__
== '__main__':
1110 lst
= ['slw 3, 1, 4',
1113 'sv.cmpi 5, 1, 3, 2',
1115 'sv.isel 64.v, 3, 2, 65.v',
1116 'sv.setb/dm=r3/sm=1<<r3 5, 31',
1117 'sv.setb/m=r3 5, 31',
1118 'sv.setb/vec2 5, 31',
1119 'sv.setb/sw=8/ew=16 5, 31',
1120 'sv.extsw./ff=eq 5, 31',
1121 'sv.extsw./satu/sz/dz/sm=r3/dm=r3 5, 31',
1122 'sv.extsw./pr=eq 5.v, 31',
1123 'sv.add. 5.v, 2.v, 1.v',
1124 'sv.add./m=r3 5.v, 2.v, 1.v',
1127 'sv.stw 5.v, 4(1.v)',
1128 'sv.ld 5.v, 4(1.v)',
1129 'setvl. 2, 3, 4, 0, 1, 1',
1130 'sv.setvl. 2, 3, 4, 0, 1, 1',
1133 "sv.stfsu 0.v, 16(4.v)",
1136 "sv.stfsu/els 0.v, 16(4)",
1139 'sv.add./mr 5.v, 2.v, 1.v',
1141 macros
= {'win2': '50', 'win': '60'}
1143 'sv.addi win2.v, win.v, -1',
1144 'sv.add./mrr 5.v, 2.v, 1.v',
1145 #'sv.lhzsh 5.v, 11(9.v), 15',
1146 #'sv.lwzsh 5.v, 11(9.v), 15',
1147 'sv.ffmadds 6.v, 2.v, 4.v, 6.v',
1150 #'sv.fmadds 0.v, 8.v, 16.v, 4.v',
1151 #'sv.ffadds 0.v, 8.v, 4.v',
1152 'svremap 11, 0, 1, 2, 3, 2, 1',
1153 'svshape 8, 1, 1, 1, 0',
1154 'svshape 8, 1, 1, 1, 1',
1157 #'sv.lfssh 4.v, 11(8.v), 15',
1158 #'sv.lwzsh 4.v, 11(8.v), 15',
1159 #'sv.svstep. 2.v, 4, 0',
1160 #'sv.fcfids. 48.v, 64.v',
1161 'sv.fcoss. 80.v, 0.v',
1162 'sv.fcoss. 20.v, 0.v',
1165 'sv.bc/all 3,12,192',
1166 'sv.bclr/vsbi 3,81.v,192',
1168 isa
= SVP64Asm(lst
, macros
=macros
)
1169 print ("list", list(isa
))