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
24 from collections
import OrderedDict
27 from openpower
.decoder
.pseudo
.pagereader
import ISA
28 from openpower
.decoder
.power_svp64
import SVP64RM
, get_regtype
, decode_extra
29 from openpower
.decoder
.selectable_int
import SelectableInt
30 from openpower
.consts
import SVP64MODE
31 from openpower
.decoder
.power_insn
import SVP64Instruction
32 from openpower
.decoder
.power_insn
import Database
33 from openpower
.decoder
.power_enums
import find_wiki_dir
36 from openpower
.util
import log
39 def instruction(*fields
):
40 def instruction(insn
, desc
):
41 (value
, start
, end
) = desc
42 bits
= ((1,) * ((end
+ 1) - start
))
45 mask
= ((mask
<< 1) | bit
)
46 return (insn |
((value
& mask
) << (31 - end
)))
48 return functools
.reduce(instruction
, fields
, 0)
54 def _insn(name
, **kwargs
):
58 def _custom_insns(*insns
):
59 """ a decorator that adds the function to `CUSTOM_INSNS` """
64 insns_
= (fn
.__name
__, {}),
67 for name
, kwargs
in insns_
:
68 if not isinstance(name
, str):
69 raise TypeError("instruction name must be a str: {name!r}")
70 if name
in CUSTOM_INSNS
:
71 raise ValueError(f
"duplicate instruction mnemonic: {name!r}")
72 # use getcallargs to check that arguments work:
73 inspect
.getcallargs(fn
, FIELDS_ARG
, **kwargs
)
74 CUSTOM_INSNS
[name
] = functools
.partial(fn
, **kwargs
)
81 _insn("setvl.", Rc
=1),
83 def setvl(fields
, Rc
):
85 setvl is a *32-bit-only* instruction. It controls SVSTATE.
86 It is *not* a 64-bit-prefixed Vector instruction (no sv.setvl, yet),
87 it is a Vector *control* instruction.
89 * setvl RT,RA,SVi,vf,vs,ms
91 1.6.28 SVL-FORM - from fields.txt
92 |0 |6 |11 |16 |23 |24 |25 |26 |31 |
93 | PO | RT | RA | SVi |ms |vs |vf | XO |Rc |
97 # ARRRGH these are in a non-obvious order in openpower/isa/simplev.mdwn
98 # compared to the SVL-Form above. sigh
99 # setvl RT,RA,SVi,vf,vs,ms
100 (RT
, RA
, SVi
, vf
, vs
, ms
) = fields
116 _insn("svstep", Rc
=0),
117 _insn("svstep.", Rc
=1),
119 def svstep(fields
, Rc
):
121 svstep is a 32-bit instruction. It updates SVSTATE.
122 It *can* be SVP64-prefixed, to indicate that its registers
127 # 1.6.28 SVL-FORM - from fields.txt
128 # |0 |6 |11 |16 |23 |24 |25 |26 |31 |
129 # | PO | RT | / | SVi |/ |/ |vf | XO |Rc |
134 (RT
, SVi
, vf
) = fields
152 svshape is a *32-bit-only* instruction. It updates SVSHAPE and SVSTATE.
153 It is *not* a 64-bit-prefixed Vector instruction (no sv.svshape, yet),
154 it is a Vector *control* instruction.
156 https://libre-soc.org/openpower/sv/remap/#svshape
158 * svshape SVxd,SVyd,SVzd,SVrm,vf
160 # 1.6.33 SVM-FORM from fields.txt
161 # |0 |6 |11 |16 |21 |25 |26 |31 |
162 # | PO | SVxd | SVyd | SVzd | SVrm |vf | XO |
164 note that SVrm is not permitted to be 0b0111, 0b1000 or 0b1001.
165 0b0111 is reserved and 0b100- is for svshape2
170 (SVxd
, SVyd
, SVzd
, SVrm
, vf
) = fields
175 # check SVrm for reserved (and svshape2) values
176 assert SVrm
not in [0b1000, 0b1001], \
177 "svshape reserved SVrm value %s" % bin(SVrm
)
191 def svshape2(fields
):
193 svshape2 is a *32-bit-only* instruction. It updates SVSHAPE and SVSTATE.
194 It is *not* a 64-bit-prefixed Vector instruction (no sv.svshape2, yet),
195 it is a Vector *control* instruction, and is a sort-of hybrid of
196 svshape and svindex, with the key important feature being the "offset".
198 https://libre-soc.org/openpower/sv/remap/discussion
200 * svshape2 SVo,SVM2yx,rmm,SVd,sk,mm
202 # 1.6.35.1 SVM2-FORM from fields.txt
203 # |0 |6 |10 |11 |16 |21 |24|25 |26 |31 |
204 # | PO | SVo |SVMyx| rmm | SVd |XO |mm|sk | XO |
206 note that this fits into the space of svshape and that XO is
207 split across 2 areas.
212 XO2
= 0b100 # not really XO2 but hey
213 (offs
, yx
, rmm
, SVd
, sk
, mm
) = fields
214 SVd
-= 1 # offset by one
218 (offs
, 6, 9), # offset (the whole point of adding svshape2)
219 (yx
, 10, 10), # like svindex
220 (rmm
, 11, 15), # ditto svindex
221 (SVd
, 16, 20), # ditto svindex
222 (XO2
, 21, 23), # actually XO split across 2 places...
223 (mm
, 24, 24), # ditto svindex
224 (sk
, 25, 25), # ditto svindex
232 svindex is a *32-bit-only* instruction. It is a convenience
233 instruction that reduces instruction count for Indexed REMAP
235 It is *not* a 64-bit-prefixed Vector instruction (no sv.svindex, yet),
236 it is a Vector *control* instruction.
239 |0 |6 |11 |16 |21 |23|24|25|26 31|
240 | PO | SVG|rmm | SVd |ew |yx|mm|sk| XO |
242 # note that the dimension field one subtracted
245 (SVG
, rmm
, SVd
, ew
, yx
, mm
, sk
) = fields
263 this is a *32-bit-only* instruction. It updates the SVSHAPE SPR
264 it is *not* a 64-bit-prefixed Vector instruction (no sv.svremap),
265 it is a Vector *control* instruction.
267 * svremap SVme,mi0,mi1,mi2,mo0,mo1,pst
270 |0 |6 |11 |13 |15 |17 |19 |21 |22 |26 |31 |
271 | PO | SVme |mi0 | mi1 | mi2 | mo0 | mo1 |pst |/// | XO |
276 (SVme
, mi0
, mi1
, mi2
, mo0
, mo1
, pst
) = fields
291 # ok from here-on down these are added as 32-bit instructions
292 # and are here only because binutils (at present) doesn't have
293 # them (that's being fixed!)
294 # they can - if implementations then choose - be Vectorised
295 # because they are general-purpose scalar instructions
300 |0 |6 |11 |16 |21 |26 |27 31|
301 | PO | RT | RA | RB |bm |L | XO |
305 (RT
, RA
, RB
, bm
, L
) = fields
317 def _fptrans_insn(name
, XO
):
319 _insn(name
, PO
=63, Rc
=0, XO
=XO
),
320 _insn(name
+ ".", PO
=63, Rc
=1, XO
=XO
),
321 _insn(name
+ "s", PO
=59, Rc
=0, XO
=XO
),
322 _insn(name
+ "s.", PO
=59, Rc
=1, XO
=XO
),
327 *_fptrans_insn("fatan2", XO
=0b1001001110),
328 *_fptrans_insn("fatan2pi", XO
=0b1000001110),
329 *_fptrans_insn("fpow", XO
=0b1111101101),
330 *_fptrans_insn("fpown", XO
=0b1101101100),
331 *_fptrans_insn("fpowr", XO
=0b1111101100),
332 *_fptrans_insn("frootn", XO
=0b1101101101),
333 *_fptrans_insn("fhypot", XO
=0b1010001110),
334 *_fptrans_insn("fminnum08", XO
=0b1011001100),
335 *_fptrans_insn("fmaxnum08", XO
=0b1011101100),
336 *_fptrans_insn("fmin19", XO
=0b1011001101),
337 *_fptrans_insn("fmax19", XO
=0b1011101101),
338 *_fptrans_insn("fminnum19", XO
=0b1011001110),
339 *_fptrans_insn("fmaxnum19", XO
=0b1011101110),
340 *_fptrans_insn("fminc", XO
=0b1011001111),
341 *_fptrans_insn("fmaxc", XO
=0b1011101111),
342 *_fptrans_insn("fminmagnum08", XO
=0b1100001110),
343 *_fptrans_insn("fmaxmagnum08", XO
=0b1100001111),
344 *_fptrans_insn("fminmag19", XO
=0b1101101110),
345 *_fptrans_insn("fmaxmag19", XO
=0b1101101111),
346 *_fptrans_insn("fminmagnum19", XO
=0b1110001110),
347 *_fptrans_insn("fmaxmagnum19", XO
=0b1110001111),
348 *_fptrans_insn("fminmagc", XO
=0b1111101110),
349 *_fptrans_insn("fmaxmagc", XO
=0b1111101111),
350 *_fptrans_insn("fmod", XO
=0b1101001111),
351 *_fptrans_insn("fremainder", XO
=0b1111001111),
353 def fptrans_binary(fields
, PO
, Rc
, XO
):
354 # XXX WARNING THESE ARE NOT APPROVED BY OPF ISA WG
355 # however we are out of space with opcode 22
357 # |0 |6 |11 |16 |21 |31 |
358 # | PO | FRT | FRA | FRB | XO |Rc |
359 # | PO | FRT | FRA | RB | XO |Rc |
360 (FRT
, FRA
, FRB
) = fields
372 *_fptrans_insn("frsqrt", XO
=0b1001001100),
373 *_fptrans_insn("fcbrt", XO
=0b1000001100),
374 *_fptrans_insn("frecip", XO
=0b1010001100),
375 *_fptrans_insn("fexp2m1", XO
=0b1100001100),
376 *_fptrans_insn("flog2p1", XO
=0b1100001101),
377 *_fptrans_insn("fexp2", XO
=0b1110001100),
378 *_fptrans_insn("flog2", XO
=0b1110001101),
379 *_fptrans_insn("fexpm1", XO
=0b1100101100),
380 *_fptrans_insn("flogp1", XO
=0b1100101101),
381 *_fptrans_insn("fexp", XO
=0b1110101100),
382 *_fptrans_insn("flog", XO
=0b1110101101),
383 *_fptrans_insn("fexp10m1", XO
=0b1101001100),
384 *_fptrans_insn("flog10p1", XO
=0b1101001101),
385 *_fptrans_insn("fexp10", XO
=0b1111001100),
386 *_fptrans_insn("flog10", XO
=0b1111001101),
387 *_fptrans_insn("fsin", XO
=0b1001001101),
388 *_fptrans_insn("fcos", XO
=0b1001101100),
389 *_fptrans_insn("ftan", XO
=0b1001101101),
390 *_fptrans_insn("fasin", XO
=0b1001001111),
391 *_fptrans_insn("facos", XO
=0b1001101110),
392 *_fptrans_insn("fatan", XO
=0b1001101111),
393 *_fptrans_insn("fsinpi", XO
=0b1000001101),
394 *_fptrans_insn("fcospi", XO
=0b1000101100),
395 *_fptrans_insn("ftanpi", XO
=0b1000101101),
396 *_fptrans_insn("fasinpi", XO
=0b1000001111),
397 *_fptrans_insn("facospi", XO
=0b1000101110),
398 *_fptrans_insn("fatanpi", XO
=0b1000101111),
399 *_fptrans_insn("fsinh", XO
=0b1010001101),
400 *_fptrans_insn("fcosh", XO
=0b1010101100),
401 *_fptrans_insn("ftanh", XO
=0b1010101101),
402 *_fptrans_insn("fasinh", XO
=0b1010001111),
403 *_fptrans_insn("facosh", XO
=0b1010101110),
404 *_fptrans_insn("fatanh", XO
=0b1010101111),
406 def fptrans_unary(fields
, PO
, Rc
, XO
):
407 # XXX WARNING THESE ARE NOT APPROVED BY OPF ISA WG
408 # however we are out of space with opcode 22
410 # |0 |6 |11 |16 |21 |31 |
411 # | PO | FRT | /// | FRB | XO |Rc |
424 _insn("ternlogi", Rc
=0),
425 _insn("ternlogi.", Rc
=1),
427 def ternlogi(fields
, Rc
):
428 # XXX WARNING THESE ARE NOT APPROVED BY OPF ISA WG
429 # however we are out of space with opcode 22
431 # |0 |6 |11 |16 |21 |29 |31 |
432 # | PO | RT | RA | RB | TLI | XO |Rc |
435 (RT
, RA
, RB
, TLI
) = fields
448 _insn("grev", Rc
=0, imm
=0, word
=0),
449 _insn("grevw", Rc
=0, imm
=0, word
=1),
450 _insn("grevi", Rc
=0, imm
=1, word
=0),
451 _insn("grevwi", Rc
=0, imm
=1, word
=1),
452 _insn("grev.", Rc
=1, imm
=0, word
=0),
453 _insn("grevw.", Rc
=1, imm
=0, word
=1),
454 _insn("grevi.", Rc
=1, imm
=1, word
=0),
455 _insn("grevwi.", Rc
=1, imm
=1, word
=1),
457 def grev(fields
, Rc
, imm
, word
):
458 # XXX WARNING THESE ARE NOT APPROVED BY OPF ISA WG
459 # however we are out of space with opcode 22
461 # _ matches fields in table at:
462 # https://libre-soc.org/openpower/sv/bitmanip/
468 (RT
, RA
, XBI
) = fields
469 insn
= (insn
<< 5) | RT
470 insn
= (insn
<< 5) | RA
473 insn
= (insn
<< 6) | XBI
474 insn
= (insn
<< 9) | XO
477 insn
= (insn
<< 5) | XBI
478 insn
= (insn
<< 10) | XO
479 insn
= (insn
<< 1) | Rc
484 _insn("maxs", XO
=0b0111001110, Rc
=0),
485 _insn("maxs.", XO
=0b0111001110, Rc
=1),
486 _insn("maxu", XO
=0b0011001110, Rc
=0),
487 _insn("maxu.", XO
=0b0011001110, Rc
=1),
488 _insn("minu", XO
=0b0001001110, Rc
=0),
489 _insn("minu.", XO
=0b0001001110, Rc
=1),
490 _insn("mins", XO
=0b0101001110, Rc
=0),
491 _insn("mins.", XO
=0b0101001110, Rc
=1),
492 _insn("absdu", XO
=0b1011110110, Rc
=0),
493 _insn("absdu.", XO
=0b1011110110, Rc
=1),
494 _insn("absds", XO
=0b1001110110, Rc
=0),
495 _insn("absds.", XO
=0b1001110110, Rc
=1),
496 _insn("avgadd", XO
=0b1101001110, Rc
=0),
497 _insn("avgadd.", XO
=0b1101001110, Rc
=1),
498 _insn("absdacu", XO
=0b1111110110, Rc
=0),
499 _insn("absdacu.", XO
=0b1111110110, Rc
=1),
500 _insn("absdacs", XO
=0b0111110110, Rc
=0),
501 _insn("absdacs.", XO
=0b0111110110, Rc
=1),
502 _insn("cprop", XO
=0b0110001110, Rc
=0),
503 _insn("cprop.", XO
=0b0110001110, Rc
=1),
505 def av(fields
, XO
, Rc
):
507 # |0 |6 |7|8|9 |10 |11|12|13 |15|16|17 |20|21 |31 |
508 # | PO | RT | RA | RB | XO |Rc |
510 (RT
, RA
, RB
) = fields
523 # XXX WARNING THESE ARE NOT APPROVED BY OPF ISA WG
524 # V3.0B 1.6.6 DX-FORM
525 # |0 |6 |7|8|9 |10 |11|12|13 |15|16|17 |26|27 |31 |
526 # | PO | FRS | d1 | d0 | XO |d2 |
530 # first split imm into d1, d0 and d2. sigh
531 d2
= (imm
& 1) # LSB (0)
532 d1
= (imm
>> 1) & 0b11111 # bits 1-5
533 d0
= (imm
>> 6) # MSBs 6-15
546 # XXX WARNING THESE ARE NOT APPROVED BY OPF ISA WG
547 # V3.0B 1.6.6 DX-FORM
548 # |0 |6 |7|8|9 |10 |11|12|13 |15|16|17 |26|27 |31 |
549 # | PO | FRS | d1 | d0 | XO |d2 |
553 # first split imm into d1, d0 and d2. sigh
554 d2
= (imm
& 1) # LSB (0)
555 d1
= (imm
>> 1) & 0b11111 # bits 1-5
556 d0
= (imm
>> 6) # MSBs 6-15
567 # decode GPR into sv extra
568 def get_extra_gpr(etype
, regmode
, field
):
569 if regmode
== 'scalar':
570 # cut into 2-bits 5-bits SS FFFFF
571 sv_extra
= field
>> 5
572 field
= field
& 0b11111
574 # cut into 5-bits 2-bits FFFFF SS
575 sv_extra
= field
& 0b11
577 return sv_extra
, field
580 # decode 3-bit CR into sv extra
581 def get_extra_cr_3bit(etype
, regmode
, field
):
582 if regmode
== 'scalar':
583 # cut into 2-bits 3-bits SS FFF
584 sv_extra
= field
>> 3
585 field
= field
& 0b111
587 # cut into 3-bits 4-bits FFF SSSS but will cut 2 zeros off later
588 sv_extra
= field
& 0b1111
590 return sv_extra
, field
594 def decode_subvl(encoding
):
595 pmap
= {'2': 0b01, '3': 0b10, '4': 0b11}
596 assert encoding
in pmap
, \
597 "encoding %s for SUBVL not recognised" % encoding
598 return pmap
[encoding
]
602 def decode_elwidth(encoding
):
603 pmap
= {'8': 0b11, '16': 0b10, '32': 0b01}
604 assert encoding
in pmap
, \
605 "encoding %s for elwidth not recognised" % encoding
606 return pmap
[encoding
]
609 # decodes predicate register encoding
610 def decode_predicate(encoding
):
621 'nl': (1, 0b001), 'ge': (1, 0b001), # same value
623 'ng': (1, 0b011), 'le': (1, 0b011), # same value
626 'so': (1, 0b110), 'un': (1, 0b110), # same value
627 'ns': (1, 0b111), 'nu': (1, 0b111), # same value
629 assert encoding
in pmap
, \
630 "encoding %s for predicate not recognised" % encoding
631 return pmap
[encoding
]
634 # decodes "Mode" in similar way to BO field (supposed to, anyway)
635 def decode_bo(encoding
):
636 pmap
= { # TODO: double-check that these are the same as Branch BO
638 'nl': 0b001, 'ge': 0b001, # same value
640 'ng': 0b011, 'le': 0b011, # same value
643 'so': 0b110, 'un': 0b110, # same value
644 'ns': 0b111, 'nu': 0b111, # same value
646 assert encoding
in pmap
, \
647 "encoding %s for BO Mode not recognised" % encoding
648 return pmap
[encoding
]
651 # partial-decode fail-first mode
652 def decode_ffirst(encoding
):
653 if encoding
in ['RC1', '~RC1']:
655 return decode_bo(encoding
)
658 def decode_reg(field
, macros
=None):
661 # decode the field number. "5.v" or "3.s" or "9"
662 # and now also "*0", and "*%0". note: *NOT* to add "*%rNNN" etc.
663 # https://bugs.libre-soc.org/show_bug.cgi?id=884#c0
664 if field
.startswith(("*%", "*")):
665 if field
.startswith("*%"):
669 while field
in macros
:
670 field
= macros
[field
]
671 return int(field
), "vector" # actual register number
673 # try old convention (to be retired)
674 field
= field
.split(".")
675 regmode
= 'scalar' # default
679 elif field
[1] == 'v':
681 field
= int(field
[0]) # actual register number
682 return field
, regmode
685 def decode_imm(field
):
686 ldst_imm
= "(" in field
and field
[-1] == ')'
688 return field
[:-1].split("(")
693 def crf_extra(etype
, rname
, extra_idx
, regmode
, field
, extras
):
694 """takes a CR Field number (CR0-CR127), splits into EXTRA2/3 and v3.0
695 the scalar/vector mode (crNN.v or crNN.s) changes both the format
696 of the EXTRA2/3 encoding as well as what range of registers is possible.
697 this function can be used for both BF/BFA and BA/BB/BT by first removing
698 the bottom 2 bits of BA/BB/BT then re-instating them after encoding.
699 see https://libre-soc.org/openpower/sv/svp64/appendix/#cr_extra
702 sv_extra
, field
= get_extra_cr_3bit(etype
, regmode
, field
)
703 # now sanity-check (and shrink afterwards)
704 if etype
== 'EXTRA2':
705 # 3-bit CR Field (BF, BFA) EXTRA2 encoding
706 if regmode
== 'scalar':
707 # range is CR0-CR15 in increments of 1
708 assert (sv_extra
>> 1) == 0, \
709 "scalar CR %s cannot fit into EXTRA2 %s" % \
710 (rname
, str(extras
[extra_idx
]))
711 # all good: encode as scalar
712 sv_extra
= sv_extra
& 0b01
714 # range is CR0-CR127 in increments of 16
715 assert sv_extra
& 0b111 == 0, \
716 "vector CR %s cannot fit into EXTRA2 %s" % \
717 (rname
, str(extras
[extra_idx
]))
718 # all good: encode as vector (bit 2 set)
719 sv_extra
= 0b10 |
(sv_extra
>> 3)
721 # 3-bit CR Field (BF, BFA) EXTRA3 encoding
722 if regmode
== 'scalar':
723 # range is CR0-CR31 in increments of 1
724 assert (sv_extra
>> 2) == 0, \
725 "scalar CR %s cannot fit into EXTRA3 %s" % \
726 (rname
, str(extras
[extra_idx
]))
727 # all good: encode as scalar
728 sv_extra
= sv_extra
& 0b11
730 # range is CR0-CR127 in increments of 8
731 assert sv_extra
& 0b11 == 0, \
732 "vector CR %s cannot fit into EXTRA3 %s" % \
733 (rname
, str(extras
[extra_idx
]))
734 # all good: encode as vector (bit 3 set)
735 sv_extra
= 0b100 |
(sv_extra
>> 2)
736 return sv_extra
, field
739 def to_number(field
):
740 if field
.startswith("0x"):
742 if field
.startswith("0b"):
747 db
= Database(find_wiki_dir())
750 # decodes svp64 assembly listings and creates EXT001 svp64 prefixes
752 def __init__(self
, lst
, bigendian
=False, macros
=None):
757 self
.trans
= self
.translate(lst
)
758 self
.isa
= ISA() # reads the v3.0B pseudo-code markdown files
759 self
.svp64
= SVP64RM() # reads the svp64 Remap entries for registers
760 assert bigendian
== False, "error, bigendian not supported yet"
763 yield from self
.trans
765 def translate_one(self
, insn
, macros
=None):
768 macros
.update(self
.macros
)
771 insn_no_comments
= insn
.partition('#')[0]
772 # find first space, to get opcode
773 ls
= insn_no_comments
.split(' ')
775 # now find opcode fields
776 fields
= ''.join(ls
[1:]).split(',')
777 mfields
= list(map(str.strip
, fields
))
778 log("opcode, fields", ls
, opcode
, mfields
)
781 for field
in mfields
:
782 fields
.append(macro_subst(macros
, field
))
783 log("opcode, fields substed", ls
, opcode
, fields
)
785 # identify if it is a special instruction
786 custom_insn_hook
= CUSTOM_INSNS
.get(opcode
)
787 if custom_insn_hook
is not None:
788 fields
= tuple(map(to_number
, fields
))
789 insn_num
= custom_insn_hook(fields
)
790 log(opcode
, bin(insn_num
))
791 yield ".long 0x%X # %s" % (insn_num
, insn
)
794 # identify if is a svp64 mnemonic
795 if not opcode
.startswith('sv.'):
796 yield insn
# unaltered
798 opcode
= opcode
[3:] # strip leading "sv"
800 # start working on decoding the svp64 op: sv.basev30Bop/vec2/mode
801 opmodes
= opcode
.split("/") # split at "/"
802 v30b_op_orig
= opmodes
.pop(0) # first is the v3.0B
803 # check instruction ends with dot
804 rc_mode
= v30b_op_orig
.endswith('.')
806 v30b_op
= v30b_op_orig
[:-1]
808 v30b_op
= v30b_op_orig
810 # look up the 32-bit op (original, with "." if it has it)
811 if v30b_op_orig
in isa
.instr
:
812 isa_instr
= isa
.instr
[v30b_op_orig
]
814 raise Exception("opcode %s of '%s' not supported" %
815 (v30b_op_orig
, insn
))
817 # look up the svp64 op, first the original (with "." if it has it)
818 if v30b_op_orig
in svp64
.instrs
:
819 rm
= svp64
.instrs
[v30b_op_orig
] # one row of the svp64 RM CSV
820 # then without the "." (if there was one)
821 elif v30b_op
in svp64
.instrs
:
822 rm
= svp64
.instrs
[v30b_op
] # one row of the svp64 RM CSV
824 raise Exception(f
"opcode {v30b_op_orig!r} of "
825 f
"{insn!r} not an svp64 instruction")
827 # get regs info e.g. "RT,RA,RB"
828 v30b_regs
= isa_instr
.regs
[0]
829 log("v3.0B op", v30b_op
, "Rc=1" if rc_mode
else '')
830 log("v3.0B regs", opcode
, v30b_regs
)
833 # right. the first thing to do is identify the ordering of
834 # the registers, by name. the EXTRA2/3 ordering is in
835 # rm['0']..rm['3'] but those fields contain the names RA, BB
836 # etc. we have to read the pseudocode to understand which
837 # reg is which in our instruction. sigh.
839 # first turn the svp64 rm into a "by name" dict, recording
840 # which position in the RM EXTRA it goes into
841 # also: record if the src or dest was a CR, for sanity-checking
842 # (elwidth overrides on CRs are banned)
843 decode
= decode_extra(rm
)
844 dest_reg_cr
, src_reg_cr
, svp64_src
, svp64_dest
= decode
846 log("EXTRA field index, src", svp64_src
)
847 log("EXTRA field index, dest", svp64_dest
)
849 # okaaay now we identify the field value (opcode N,N,N) with
850 # the pseudo-code info (opcode RT, RA, RB)
851 assert len(fields
) == len(v30b_regs
), \
852 "length of fields %s must match insn `%s` fields %s" % \
853 (str(v30b_regs
), insn
, str(fields
))
854 opregfields
= zip(fields
, v30b_regs
) # err that was easy
856 # now for each of those find its place in the EXTRA encoding
857 # note there is the possibility (for LD/ST-with-update) of
858 # RA occurring **TWICE**. to avoid it getting added to the
859 # v3.0B suffix twice, we spot it as a duplicate, here
860 extras
= OrderedDict()
861 for idx
, (field
, regname
) in enumerate(opregfields
):
862 imm
, regname
= decode_imm(regname
)
863 rtype
= get_regtype(regname
)
864 log(" idx find", rtype
, idx
, field
, regname
, imm
)
866 # probably an immediate field, append it straight
867 extras
[('imm', idx
, False)] = (idx
, field
, None, None, None)
869 extra
= svp64_src
.get(regname
, None)
870 if extra
is not None:
871 extra
= ('s', extra
, False) # not a duplicate
872 extras
[extra
] = (idx
, field
, regname
, rtype
, imm
)
873 log(" idx src", idx
, extra
, extras
[extra
])
874 dextra
= svp64_dest
.get(regname
, None)
875 log("regname in", regname
, dextra
)
876 if dextra
is not None:
877 is_a_duplicate
= extra
is not None # duplicate spotted
878 dextra
= ('d', dextra
, is_a_duplicate
)
879 extras
[dextra
] = (idx
, field
, regname
, rtype
, imm
)
880 log(" idx dst", idx
, extra
, extras
[dextra
])
882 # great! got the extra fields in their associated positions:
883 # also we know the register type. now to create the EXTRA encodings
884 etype
= rm
['Etype'] # Extra type: EXTRA3/EXTRA2
885 ptype
= rm
['Ptype'] # Predication type: Twin / Single
888 for extra_idx
, (idx
, field
, rname
, rtype
, iname
) in extras
.items():
889 # is it a field we don't alter/examine? if so just put it
892 v30b_newfields
.append(field
)
895 # identify if this is a ld/st immediate(reg) thing
896 ldst_imm
= "(" in field
and field
[-1] == ')'
898 immed
, field
= field
[:-1].split("(")
900 field
, regmode
= decode_reg(field
, macros
=macros
)
901 log(" ", extra_idx
, rname
, rtype
,
902 regmode
, iname
, field
, end
=" ")
904 # see Mode field https://libre-soc.org/openpower/sv/svp64/
905 # XXX TODO: the following is a bit of a laborious repeated
906 # mess, which could (and should) easily be parameterised.
907 # XXX also TODO: the LD/ST modes which are different
908 # https://libre-soc.org/openpower/sv/ldst/
910 # rright. SVP64 register numbering is from 0 to 127
911 # for GPRs, FPRs *and* CR Fields, where for v3.0 the GPRs and RPFs
912 # are 0-31 and CR Fields are only 0-7. the SVP64 RM "Extra"
913 # area is used to extend the numbering from the 32-bit
914 # instruction, and also to record whether the register
915 # is scalar or vector. on a per-operand basis. this
916 # results in a slightly finnicky encoding: here we go...
918 # encode SV-GPR and SV-FPR field into extra, v3.0field
919 if rtype
in ['GPR', 'FPR']:
920 sv_extra
, field
= get_extra_gpr(etype
, regmode
, field
)
921 # now sanity-check. EXTRA3 is ok, EXTRA2 has limits
922 # (and shrink to a single bit if ok)
923 if etype
== 'EXTRA2':
924 if regmode
== 'scalar':
925 # range is r0-r63 in increments of 1
926 assert (sv_extra
>> 1) == 0, \
927 "scalar GPR %s cannot fit into EXTRA2 %s" % \
928 (rname
, str(extras
[extra_idx
]))
929 # all good: encode as scalar
930 sv_extra
= sv_extra
& 0b01
932 # range is r0-r127 in increments of 2 (r0 r2 ... r126)
933 assert sv_extra
& 0b01 == 0, \
934 "%s: vector field %s cannot fit " \
936 (insn
, rname
, str(extras
[extra_idx
]))
937 # all good: encode as vector (bit 2 set)
938 sv_extra
= 0b10 |
(sv_extra
>> 1)
939 elif regmode
== 'vector':
940 # EXTRA3 vector bit needs marking
943 # encode SV-CR 3-bit field into extra, v3.0field.
944 # 3-bit is for things like BF and BFA
945 elif rtype
== 'CR_3bit':
946 sv_extra
, field
= crf_extra(etype
, rname
, extra_idx
,
947 regmode
, field
, extras
)
949 # encode SV-CR 5-bit field into extra, v3.0field
950 # 5-bit is for things like BA BB BC BT etc.
951 # *sigh* this is the same as 3-bit except the 2 LSBs of the
952 # 5-bit field are passed through unaltered.
953 elif rtype
== 'CR_5bit':
954 cr_subfield
= field
& 0b11 # record bottom 2 bits for later
955 field
= field
>> 2 # strip bottom 2 bits
956 # use the exact same 3-bit function for the top 3 bits
957 sv_extra
, field
= crf_extra(etype
, rname
, extra_idx
,
958 regmode
, field
, extras
)
959 # reconstruct the actual 5-bit CR field (preserving the
960 # bottom 2 bits, unaltered)
961 field
= (field
<< 2) | cr_subfield
964 raise Exception("no type match: %s" % rtype
)
966 # capture the extra field info
967 log("=>", "%5s" % bin(sv_extra
), field
)
968 extras
[extra_idx
] = sv_extra
970 # append altered field value to v3.0b, differs for LDST
971 # note that duplicates are skipped e.g. EXTRA2 contains
972 # *BOTH* s:RA *AND* d:RA which happens on LD/ST-with-update
973 srcdest
, idx
, duplicate
= extra_idx
974 if duplicate
: # skip adding to v3.0b fields, already added
977 v30b_newfields
.append(("%s(%s)" % (immed
, str(field
))))
979 v30b_newfields
.append(str(field
))
981 log("new v3.0B fields", v30b_op
, v30b_newfields
)
982 log("extras", extras
)
984 # rright. now we have all the info. start creating SVP64 instruction.
985 svp64_insn
= SVP64Instruction
.pair(prefix
=0, suffix
=0)
986 svp64_prefix
= svp64_insn
.prefix
987 svp64_rm
= svp64_insn
.prefix
.rm
989 # begin with EXTRA fields
990 for idx
, sv_extra
in extras
.items():
996 srcdest
, idx
, duplicate
= idx
997 if etype
== 'EXTRA2':
998 svp64_rm
.extra2
[idx
] = sv_extra
1000 svp64_rm
.extra3
[idx
] = sv_extra
1002 # identify if the op is a LD/ST. the "blegh" way. copied
1003 # from power_enums. TODO, split the list _insns down.
1004 is_ld
= v30b_op
in [
1005 "lbarx", "lbz", "lbzu", "lbzux", "lbzx", # load byte
1006 "ld", "ldarx", "ldbrx", "ldu", "ldux", "ldx", # load double
1007 "lfs", "lfsx", "lfsu", "lfsux", # FP load single
1008 "lfd", "lfdx", "lfdu", "lfdux", "lfiwzx", "lfiwax", # FP load dbl
1009 "lha", "lharx", "lhau", "lhaux", "lhax", # load half
1010 "lhbrx", "lhz", "lhzu", "lhzux", "lhzx", # more load half
1011 "lwa", "lwarx", "lwaux", "lwax", "lwbrx", # load word
1012 "lwz", "lwzcix", "lwzu", "lwzux", "lwzx", # more load word
1014 is_st
= v30b_op
in [
1015 "stb", "stbcix", "stbcx", "stbu", "stbux", "stbx",
1016 "std", "stdbrx", "stdcx", "stdu", "stdux", "stdx",
1017 "stfs", "stfsx", "stfsu", "stfux", # FP store sgl
1018 "stfd", "stfdx", "stfdu", "stfdux", "stfiwx", # FP store dbl
1019 "sth", "sthbrx", "sthcx", "sthu", "sthux", "sthx",
1020 "stw", "stwbrx", "stwcx", "stwu", "stwux", "stwx",
1022 # use this to determine if the SVP64 RM format is different.
1023 # see https://libre-soc.org/openpower/sv/ldst/
1024 is_ldst
= is_ld
or is_st
1026 # branch-conditional detection
1027 is_bc
= v30b_op
in [
1033 pmask
= 0 # bits 1-3
1034 destwid
= 0 # bits 4-5
1035 srcwid
= 0 # bits 6-7
1036 subvl
= 0 # bits 8-9
1037 smask
= 0 # bits 16-18 but only for twin-predication
1038 mode
= 0 # bits 19-23
1040 mask_m_specified
= False
1050 reverse_gear
= False
1051 mapreduce_crm
= False
1052 mapreduce_svm
= False
1058 # branch-conditional bits
1068 # ok let's start identifying opcode augmentation fields
1069 for encmode
in opmodes
:
1070 # predicate mask (src and dest)
1071 if encmode
.startswith("m="):
1073 pmmode
, pmask
= decode_predicate(encmode
[2:])
1074 smmode
, smask
= pmmode
, pmask
1076 mask_m_specified
= True
1077 # predicate mask (dest)
1078 elif encmode
.startswith("dm="):
1080 pmmode
, pmask
= decode_predicate(encmode
[3:])
1083 # predicate mask (src, twin-pred)
1084 elif encmode
.startswith("sm="):
1086 smmode
, smask
= decode_predicate(encmode
[3:])
1090 elif encmode
.startswith("vec"):
1091 subvl
= decode_subvl(encmode
[3:])
1093 elif encmode
.startswith("ew="):
1094 destwid
= decode_elwidth(encmode
[3:])
1095 elif encmode
.startswith("sw="):
1096 srcwid
= decode_elwidth(encmode
[3:])
1097 # HACK! using destwid for pack/unpack TODO, separate setvl RM
1098 elif encmode
== 'pk':
1100 elif encmode
== 'up':
1102 elif encmode
== 'pu':
1104 # element-strided LD/ST
1105 elif encmode
== 'els':
1108 elif encmode
== 'sats':
1109 assert sv_mode
is None
1112 elif encmode
== 'satu':
1113 assert sv_mode
is None
1117 elif encmode
== 'sz':
1119 elif encmode
== 'dz':
1122 elif encmode
.startswith("ff="):
1123 assert sv_mode
is None
1125 failfirst
= decode_ffirst(encmode
[3:])
1126 # predicate-result, interestingly same as fail-first
1127 elif encmode
.startswith("pr="):
1128 assert sv_mode
is None
1130 predresult
= decode_ffirst(encmode
[3:])
1131 # map-reduce mode, reverse-gear
1132 elif encmode
== 'mrr':
1133 assert sv_mode
is None
1138 elif encmode
== 'mr':
1139 assert sv_mode
is None
1142 elif encmode
== 'crm': # CR on map-reduce
1143 assert sv_mode
is None
1145 mapreduce_crm
= True
1146 elif encmode
== 'svm': # sub-vector mode
1147 mapreduce_svm
= True
1149 if encmode
== 'all':
1151 elif encmode
== 'st': # svstep mode
1153 elif encmode
== 'sr': # svstep BRc mode
1156 elif encmode
== 'vs': # VLSET mode
1158 elif encmode
== 'vsi': # VLSET mode with VLI (VL inclusives)
1161 elif encmode
== 'vsb': # VLSET mode with VSb
1164 elif encmode
== 'vsbi': # VLSET mode with VLI and VSb
1168 elif encmode
== 'snz': # sz (only) already set above
1171 elif encmode
== 'lu': # LR update mode
1174 raise AssertionError("unknown encmode %s" % encmode
)
1176 raise AssertionError("unknown encmode %s" % encmode
)
1179 # since m=xx takes precedence (overrides) sm=xx and dm=xx,
1180 # treat them as mutually exclusive
1181 if mask_m_specified
:
1182 assert not has_smask
,\
1183 "cannot have both source-mask and predicate mask"
1184 assert not has_pmask
,\
1185 "cannot have both dest-mask and predicate mask"
1186 # since the default is INT predication (ALWAYS), if you
1187 # specify one CR mask, you must specify both, to avoid
1188 # mixing INT and CR reg types
1189 if has_pmask
and pmmode
== 1:
1191 "need explicit source-mask in CR twin predication"
1192 if has_smask
and smmode
== 1:
1194 "need explicit dest-mask in CR twin predication"
1195 # sanity-check that 2Pred mask is same mode
1196 if has_pmask
and has_smask
:
1197 assert smmode
== pmmode
, \
1198 "predicate masks %s and %s must be same reg type" % \
1201 # sanity-check that twin-predication mask only specified in 2P mode
1203 assert not has_smask
, \
1204 "source-mask can only be specified on Twin-predicate ops"
1205 assert not has_pmask
, \
1206 "dest-mask can only be specified on Twin-predicate ops"
1208 # construct the mode field, doing sanity-checking along the way
1210 assert sv_mode
== 0b00, "sub-vector mode in mapreduce only"
1211 assert subvl
!= 0, "sub-vector mode not possible on SUBVL=1"
1214 assert has_smask
or mask_m_specified
, \
1215 "src zeroing requires a source predicate"
1217 assert has_pmask
or mask_m_specified
, \
1218 "dest zeroing requires a dest predicate"
1220 # okaaay, so there are 4 different modes, here, which will be
1221 # partly-merged-in: is_ldst is merged in with "normal", but
1222 # is_bc is so different it's done separately. likewise is_cr
1223 # (when it is done). here are the maps:
1225 # for "normal" arithmetic: https://libre-soc.org/openpower/sv/normal/
1227 | 0-1 | 2 | 3 4 | description |
1228 | --- | --- |---------|-------------------------- |
1229 | 00 | 0 | dz sz | simple mode |
1230 | 00 | 1 | 0 RG | scalar reduce mode (mapreduce), SUBVL=1 |
1231 | 00 | 1 | SVM 0 | subvector reduce mode, SUBVL>1 |
1232 | 00 | 1 | / 1 | reserved |
1233 | 01 | inv | CR-bit | Rc=1: ffirst CR sel |
1234 | 01 | inv | VLi RC1 | Rc=0: ffirst z/nonz |
1235 | 10 | N | dz sz | sat mode: N=0/1 u/s |
1236 | 11 | inv | CR-bit | Rc=1: pred-result CR sel |
1237 | 11 | inv | zz RC1 | Rc=0: pred-result z/nonz |
1240 # https://libre-soc.org/openpower/sv/ldst/
1241 # for LD/ST-immediate:
1243 | 0-1 | 2 | 3 4 | description |
1244 | --- | --- |---------|--------------------------- |
1245 | 00 | 0 | dz els | normal mode |
1246 | 00 | 1 | dz shf | shift mode |
1247 | 01 | inv | CR-bit | Rc=1: ffirst CR sel |
1248 | 01 | inv | els RC1 | Rc=0: ffirst z/nonz |
1249 | 10 | N | dz els | sat mode: N=0/1 u/s |
1250 | 11 | inv | CR-bit | Rc=1: pred-result CR sel |
1251 | 11 | inv | els RC1 | Rc=0: pred-result z/nonz |
1254 # for LD/ST-indexed (RA+RB):
1256 | 0-1 | 2 | 3 4 | description |
1257 | --- | --- |---------|-------------------------- |
1258 | 00 | SEA | dz sz | normal mode |
1259 | 01 | SEA | dz sz | Strided (scalar only source) |
1260 | 10 | N | dz sz | sat mode: N=0/1 u/s |
1261 | 11 | inv | CR-bit | Rc=1: pred-result CR sel |
1262 | 11 | inv | dz RC1 | Rc=0: pred-result z/nonz |
1265 # and leaving out branches and cr_ops for now because they're
1267 """ TODO branches and cr_ops
1270 # now create mode and (overridden) src/dst widths
1271 # XXX TODO: sanity-check bc modes
1273 sv_mode
= ((bc_svstep
<< SVP64MODE
.MOD2_MSB
) |
1274 (bc_vlset
<< SVP64MODE
.MOD2_LSB
) |
1275 (bc_snz
<< SVP64MODE
.BC_SNZ
))
1276 srcwid
= (bc_vsb
<< 1) | bc_lru
1277 destwid
= (bc_lru
<< 1) | bc_all
1281 ######################################
1284 mode |
= src_zero
<< SVP64MODE
.SZ
# predicate zeroing
1285 mode |
= dst_zero
<< SVP64MODE
.DZ
# predicate zeroing
1287 # TODO: for now, LD/ST-indexed is ignored.
1288 mode |
= ldst_elstride
<< SVP64MODE
.ELS_NORMAL
# el-strided
1290 # TODO, reduce and subvector mode
1291 # 00 1 dz CRM reduce mode (mapreduce), SUBVL=1
1292 # 00 1 SVM CRM subvector reduce mode, SUBVL>1
1296 ######################################
1298 elif sv_mode
== 0b00:
1299 mode |
= (0b1 << SVP64MODE
.REDUCE
) # sets mapreduce
1300 assert dst_zero
== 0, "dest-zero not allowed in mapreduce mode"
1302 mode |
= (0b1 << SVP64MODE
.RG
) # sets Reverse-gear mode
1304 mode |
= (0b1 << SVP64MODE
.CRM
) # sets CRM mode
1305 assert rc_mode
, "CRM only allowed when Rc=1"
1306 # bit of weird encoding to jam zero-pred or SVM mode in.
1307 # SVM mode can be enabled only when SUBVL=2/3/4 (vec2/3/4)
1309 mode |
= dst_zero
<< SVP64MODE
.DZ
# predicate zeroing
1311 mode |
= (0b1 << SVP64MODE
.SVM
) # sets SVM mode
1313 ######################################
1315 elif sv_mode
== 0b01:
1316 assert src_zero
== 0, "dest-zero not allowed in failfirst mode"
1317 if failfirst
== 'RC1':
1318 mode |
= (0b1 << SVP64MODE
.RC1
) # sets RC1 mode
1319 mode |
= (dst_zero
<< SVP64MODE
.DZ
) # predicate dst-zeroing
1320 assert rc_mode
== False, "ffirst RC1 only ok when Rc=0"
1321 elif failfirst
== '~RC1':
1322 mode |
= (0b1 << SVP64MODE
.RC1
) # sets RC1 mode
1323 mode |
= (dst_zero
<< SVP64MODE
.DZ
) # predicate dst-zeroing
1324 mode |
= (0b1 << SVP64MODE
.INV
) # ... with inversion
1325 assert rc_mode
== False, "ffirst RC1 only ok when Rc=0"
1327 assert dst_zero
== 0, "dst-zero not allowed in ffirst BO"
1328 assert rc_mode
, "ffirst BO only possible when Rc=1"
1329 mode |
= (failfirst
<< SVP64MODE
.BO_LSB
) # set BO
1331 ######################################
1332 # "saturation" modes
1333 elif sv_mode
== 0b10:
1334 mode |
= src_zero
<< SVP64MODE
.SZ
# predicate zeroing
1335 mode |
= dst_zero
<< SVP64MODE
.DZ
# predicate zeroing
1336 mode |
= (saturation
<< SVP64MODE
.N
) # signed/us saturation
1338 ######################################
1339 # "predicate-result" modes. err... code-duplication from ffirst
1340 elif sv_mode
== 0b11:
1341 assert src_zero
== 0, "dest-zero not allowed in predresult mode"
1342 if predresult
== 'RC1':
1343 mode |
= (0b1 << SVP64MODE
.RC1
) # sets RC1 mode
1344 mode |
= (dst_zero
<< SVP64MODE
.DZ
) # predicate dst-zeroing
1345 assert rc_mode
== False, "pr-mode RC1 only ok when Rc=0"
1346 elif predresult
== '~RC1':
1347 mode |
= (0b1 << SVP64MODE
.RC1
) # sets RC1 mode
1348 mode |
= (dst_zero
<< SVP64MODE
.DZ
) # predicate dst-zeroing
1349 mode |
= (0b1 << SVP64MODE
.INV
) # ... with inversion
1350 assert rc_mode
== False, "pr-mode RC1 only ok when Rc=0"
1352 assert dst_zero
== 0, "dst-zero not allowed in pr-mode BO"
1353 assert rc_mode
, "pr-mode BO only possible when Rc=1"
1354 mode |
= (predresult
<< SVP64MODE
.BO_LSB
) # set BO
1356 # whewww.... modes all done :)
1357 # now put into svp64_rm
1360 svp64_rm
.mode
= mode
1362 # put in predicate masks into svp64_rm
1364 # source pred: bits 16-18
1365 svp64_rm
.smask
= smask
1367 svp64_rm
.mmode
= mmode
1369 svp64_rm
.mask
= pmask
1371 # and subvl: bits 8-9
1372 svp64_rm
.subvl
= subvl
1376 svp64_rm
.ewsrc
= srcwid
1378 svp64_rm
.elwidth
= destwid
1380 # nice debug printout. (and now for something completely different)
1381 # https://youtu.be/u0WOIwlXE9g?t=146
1382 svp64_rm_value
= int(svp64_rm
)
1383 log("svp64_rm", hex(svp64_rm_value
), bin(svp64_rm_value
))
1384 log(" mmode 0 :", bin(mmode
))
1385 log(" pmask 1-3 :", bin(pmask
))
1386 log(" dstwid 4-5 :", bin(destwid
))
1387 log(" srcwid 6-7 :", bin(srcwid
))
1388 log(" subvl 8-9 :", bin(subvl
))
1389 log(" mode 19-23:", bin(mode
))
1390 offs
= 2 if etype
== 'EXTRA2' else 3 # 2 or 3 bits
1391 for idx
, sv_extra
in extras
.items():
1396 srcdest
, idx
, duplicate
= idx
1397 start
= (10+idx
*offs
)
1398 end
= start
+ offs
-1
1399 log(" extra%d %2d-%2d:" % (idx
, start
, end
),
1402 log(" smask 16-17:", bin(smask
))
1405 # update prefix PO and ID (aka PID)
1406 svp64_prefix
.po
= 0x1
1407 svp64_prefix
.id = 0b11
1409 # fiinally yield the svp64 prefix and the thingy. v3.0b opcode
1410 rc
= '.' if rc_mode
else ''
1411 yield ".long 0x%08x" % int(svp64_prefix
)
1412 log(v30b_op
, v30b_newfields
)
1414 v30b_op_rc
= v30b_op
1415 if not v30b_op
.endswith('.'):
1419 # FIXME(lkcl): should sv.svstep be like svstep?
1420 if v30b_op_rc
in ("svstep", "svstep."):
1421 # compensate for `SVi -= 1` in svstep()
1422 v30b_newfields
[1] = str(int(v30b_newfields
[1]) + 1)
1424 custom_insn_hook
= CUSTOM_INSNS
.get(v30b_op_rc
)
1425 if custom_insn_hook
is not None:
1426 fields
= tuple(map(to_number
, v30b_newfields
))
1427 insn_num
= custom_insn_hook(fields
)
1428 log(opcode
, bin(insn_num
))
1429 yield ".long 0x%X # %s" % (insn_num
, insn
)
1431 # argh, sv.fmadds etc. need to be done manually
1432 elif v30b_op
== 'ffmadds':
1433 opcode
= 59 << (32-6) # bits 0..6 (MSB0)
1434 opcode |
= int(v30b_newfields
[0]) << (32-11) # FRT
1435 opcode |
= int(v30b_newfields
[1]) << (32-16) # FRA
1436 opcode |
= int(v30b_newfields
[2]) << (32-21) # FRB
1437 opcode |
= int(v30b_newfields
[3]) << (32-26) # FRC
1438 opcode |
= 0b00101 << (32-31) # bits 26-30
1440 opcode |
= 1 # Rc, bit 31.
1441 yield ".long 0x%x" % opcode
1442 # argh, sv.fdmadds need to be done manually
1443 elif v30b_op
== 'fdmadds':
1444 opcode
= 59 << (32-6) # bits 0..6 (MSB0)
1445 opcode |
= int(v30b_newfields
[0]) << (32-11) # FRT
1446 opcode |
= int(v30b_newfields
[1]) << (32-16) # FRA
1447 opcode |
= int(v30b_newfields
[2]) << (32-21) # FRB
1448 opcode |
= int(v30b_newfields
[3]) << (32-26) # FRC
1449 opcode |
= 0b11011 << (32-31) # bits 26-30
1451 opcode |
= 1 # Rc, bit 31.
1452 yield ".long 0x%x" % opcode
1453 # argh, sv.ffadds etc. need to be done manually
1454 elif v30b_op
== 'ffadds':
1455 opcode
= 59 << (32-6) # bits 0..6 (MSB0)
1456 opcode |
= int(v30b_newfields
[0]) << (32-11) # FRT
1457 opcode |
= int(v30b_newfields
[1]) << (32-16) # FRA
1458 opcode |
= int(v30b_newfields
[2]) << (32-21) # FRB
1459 opcode |
= 0b1111100000 << (32-31) # bits 21-30
1461 opcode |
= 1 # Rc, bit 31.
1462 yield ".long 0x%x" % opcode
1464 if not v30b_op
.endswith('.'):
1466 yield "%s %s" % (v30b_op
, ", ".join(v30b_newfields
))
1467 log("new v3.0B fields", v30b_op
, v30b_newfields
)
1469 def translate(self
, lst
):
1471 yield from self
.translate_one(insn
)
1474 def macro_subst(macros
, txt
):
1476 log("subst", txt
, macros
)
1479 for macro
, value
in macros
.items():
1482 replaced
= txt
.replace(macro
, value
)
1483 log("macro", txt
, "replaced", replaced
, macro
, value
)
1486 toreplace
= '%s.s' % macro
1487 if toreplace
== txt
:
1489 replaced
= txt
.replace(toreplace
, "%s.s" % value
)
1490 log("macro", txt
, "replaced", replaced
, toreplace
, value
)
1493 toreplace
= '%s.v' % macro
1494 if toreplace
== txt
:
1496 replaced
= txt
.replace(toreplace
, "%s.v" % value
)
1497 log("macro", txt
, "replaced", replaced
, toreplace
, value
)
1500 toreplace
= '(%s)' % macro
1501 if toreplace
in txt
:
1503 replaced
= txt
.replace(toreplace
, '(%s)' % value
)
1504 log("macro", txt
, "replaced", replaced
, toreplace
, value
)
1507 log(" processed", txt
)
1515 if not line
[0].isspace():
1523 # get an input file and an output file
1527 outfile
= sys
.stdout
1528 # read the whole lot in advance in case of in-place
1529 lines
= list(infile
.readlines())
1530 elif len(args
) != 2:
1531 print("pysvp64asm [infile | -] [outfile | -]", file=sys
.stderr
)
1537 infile
= open(args
[0], "r")
1538 # read the whole lot in advance in case of in-place overwrite
1539 lines
= list(infile
.readlines())
1542 outfile
= sys
.stdout
1544 outfile
= open(args
[1], "w")
1546 # read the line, look for custom insn, process it
1547 macros
= {} # macros which start ".set"
1550 op
= line
.split("#")[0].strip()
1552 if op
.startswith(".set"):
1553 macro
= op
[4:].split(",")
1554 (macro
, value
) = map(str.strip
, macro
)
1555 macros
[macro
] = value
1556 if not op
.startswith('sv.') and not op
.startswith(tuple(CUSTOM_INSNS
)):
1560 (ws
, line
) = get_ws(line
)
1561 lst
= isa
.translate_one(op
, macros
)
1562 lst
= '; '.join(lst
)
1563 outfile
.write("%s%s # %s\n" % (ws
, lst
, op
))
1566 if __name__
== '__main__':
1567 lst
= ['slw 3, 1, 4',
1570 'sv.cmpi 5, 1, 3, 2',
1572 'sv.isel 64.v, 3, 2, 65.v',
1573 'sv.setb/dm=r3/sm=1<<r3 5, 31',
1574 'sv.setb/m=r3 5, 31',
1575 'sv.setb/vec2 5, 31',
1576 'sv.setb/sw=8/ew=16 5, 31',
1577 'sv.extsw./ff=eq 5, 31',
1578 'sv.extsw./satu/sz/dz/sm=r3/dm=r3 5, 31',
1579 'sv.extsw./pr=eq 5.v, 31',
1580 'sv.add. 5.v, 2.v, 1.v',
1581 'sv.add./m=r3 5.v, 2.v, 1.v',
1584 'sv.stw 5.v, 4(1.v)',
1585 'sv.ld 5.v, 4(1.v)',
1586 'setvl. 2, 3, 4, 0, 1, 1',
1587 'sv.setvl. 2, 3, 4, 0, 1, 1',
1590 "sv.stfsu 0.v, 16(4.v)",
1593 "sv.stfsu/els 0.v, 16(4)",
1596 'sv.add./mr 5.v, 2.v, 1.v',
1598 macros
= {'win2': '50', 'win': '60'}
1600 'sv.addi win2.v, win.v, -1',
1601 'sv.add./mrr 5.v, 2.v, 1.v',
1602 #'sv.lhzsh 5.v, 11(9.v), 15',
1603 #'sv.lwzsh 5.v, 11(9.v), 15',
1604 'sv.ffmadds 6.v, 2.v, 4.v, 6.v',
1607 #'sv.fmadds 0.v, 8.v, 16.v, 4.v',
1608 #'sv.ffadds 0.v, 8.v, 4.v',
1609 'svremap 11, 0, 1, 2, 3, 2, 1',
1610 'svshape 8, 1, 1, 1, 0',
1611 'svshape 8, 1, 1, 1, 1',
1614 #'sv.lfssh 4.v, 11(8.v), 15',
1615 #'sv.lwzsh 4.v, 11(8.v), 15',
1616 #'sv.svstep. 2.v, 4, 0',
1617 #'sv.fcfids. 48.v, 64.v',
1618 'sv.fcoss. 80.v, 0.v',
1619 'sv.fcoss. 20.v, 0.v',
1622 'sv.bc/all 3,12,192',
1623 'sv.bclr/vsbi 3,81.v,192',
1624 'sv.ld 5.v, 4(1.v)',
1625 'sv.svstep. 2.v, 4, 0',
1636 'svindex 0,0,1,0,0,0,0',
1639 'sv.svstep./m=r3 2.v, 4, 0',
1640 'ternlogi 0,0,0,0x5',
1652 'sv.andi. *80, *80, 1',
1653 'sv.ffmadds. 6.v, 2.v, 4.v, 6.v', # incorrectly inserted 32-bit op
1654 'sv.ffmadds 6.v, 2.v, 4.v, 6.v', # correctly converted to .long
1655 'svshape2 8, 1, 31, 7, 1, 1',
1658 'sv.setvl 2, 3, 4, 0, 1, 1',
1659 'sv.setvl/pk 2, 3, 4, 0, 1, 1',
1660 'sv.setvl/up 2, 3, 4, 0, 1, 1',
1661 'sv.setvl/pu 2, 3, 4, 0, 1, 1',
1663 isa
= SVP64Asm(lst
, macros
=macros
)
1664 log("list:\n", "\n\t".join(list(isa
)))
1665 # running svp64.py is designed to test hard-coded lists
1666 # (above) - which strictly speaking should all be unit tests.
1667 # if you need to actually do assembler translation at the
1668 # commandline use "pysvp64asm" - see setup.py
1669 # XXX NO. asm_process()