1 ! (c)2010 Joe Groff bsd license
2 USING: accessors arrays combinators io io.streams.string kernel
3 math math.parser roles sequences strings variants words ;
6 UNION: dim integer sequence ;
14 .texref .samplerref .surfref
15 .v2: { { of ptx-type } }
16 .v4: { { of ptx-type } }
17 .struct: { { name string } } ;
20 sm_10 sm_11 sm_12 sm_13 sm_20 ;
23 .texmode_unified .texmode_independent ;
25 VARIANT: ptx-storage-space
28 .const: { { bank maybe{ integer } } }
35 ROLE-TUPLE: ptx-target
36 { arch maybe{ ptx-arch } }
37 { map_f64_to_f32? boolean }
38 { texmode maybe{ ptx-texmode } } ;
45 ROLE-TUPLE: ptx-struct-definition
49 ROLE-TUPLE: ptx-variable
52 { align maybe{ integer } }
53 { storage-space ptx-storage-space }
56 { parameter maybe{ integer } }
58 { initializer maybe{ string } } ;
60 ROLE-TUPLE: ptx-negation
63 ROLE-TUPLE: ptx-vector
66 ROLE-TUPLE: ptx-element
73 ROLE-TUPLE: ptx-indirect
78 integer float ptx-var ptx-negation ptx-vector ptx-indirect ;
80 ROLE-TUPLE: ptx-instruction
81 { label maybe{ string } }
82 { predicate maybe{ ptx-operand } } ;
90 ROLE-TUPLE: ptx-func < ptx-entry
91 { return maybe{ ptx-variable } } ;
93 ROLE-TUPLE: ptx-directive ;
95 ROLE-TUPLE: .file < ptx-directive
97 ROLE-TUPLE: .loc < ptx-directive
99 ROLE-TUPLE: .maxnctapersm < ptx-directive
101 ROLE-TUPLE: .minnctapersm < ptx-directive
103 ROLE-TUPLE: .maxnreg < ptx-directive
105 ROLE-TUPLE: .maxntid < ptx-directive
107 ROLE-TUPLE: .pragma < ptx-directive
110 VARIANT: ptx-float-rounding-mode
111 .rn .rz .rm .rp .approx .full ;
112 VARIANT: ptx-int-rounding-mode
113 .rni .rzi .rmi .rpi ;
115 UNION: ptx-rounding-mode
116 ptx-float-rounding-mode ptx-int-rounding-mode ;
118 ROLE-TUPLE: ptx-typed-instruction < ptx-instruction
120 { dest ptx-operand } ;
122 ROLE-TUPLE: ptx-2op-instruction < ptx-typed-instruction
125 ROLE-TUPLE: ptx-3op-instruction < ptx-typed-instruction
129 ROLE-TUPLE: ptx-4op-instruction < ptx-typed-instruction
134 ROLE-TUPLE: ptx-5op-instruction < ptx-typed-instruction
140 ROLE-TUPLE: ptx-addsub-instruction < ptx-3op-instruction
144 VARIANT: ptx-mul-mode
147 ROLE-TUPLE: ptx-mul-instruction < ptx-3op-instruction
148 { mode maybe{ ptx-mul-mode } } ;
150 ROLE-TUPLE: ptx-mad-instruction < ptx-4op-instruction
151 { mode maybe{ ptx-mul-mode } }
154 VARIANT: ptx-prmt-mode
155 .f4e .b4e .rc8 .ecl .ecr .rc16 ;
159 ROLE: ptx-float-env < ptx-float-ftz
160 { round maybe{ ptx-float-rounding-mode } } ;
162 VARIANT: ptx-testp-op
163 .finite .infinite .number .notanumber .normal .subnormal ;
174 .and .or .xor .cas .exch .add .inc .dec .min .max
177 SINGLETONS: .lo .hi ;
178 INSTANCE: .lo ptx-mul-mode
179 INSTANCE: .lo ptx-cmp-op
180 INSTANCE: .hi ptx-mul-mode
181 INSTANCE: .hi ptx-cmp-op
183 ROLE-TUPLE: ptx-set-instruction < ptx-3op-instruction
184 { cmp-op ptx-cmp-op }
185 { bool-op maybe{ ptx-op } }
186 { c maybe{ ptx-operand } }
189 VARIANT: ptx-cache-op
193 ROLE-TUPLE: ptx-ldst-instruction < ptx-2op-instruction
194 { volatile? boolean }
195 { storage-space maybe{ ptx-storage-space } }
196 { cache-op maybe{ ptx-cache-op } } ;
198 VARIANT: ptx-cache-level
201 ROLE-TUPLE: ptx-branch-instruction < ptx-instruction
205 VARIANT: ptx-membar-level
208 VARIANT: ptx-vote-mode
209 .all .any .uni .ballot ;
211 ROLE-TUPLE: ptx-instruction-not-supported-yet < ptx-instruction ;
213 ROLE-TUPLE: abs <{ ptx-2op-instruction ptx-float-ftz } ;
214 ROLE-TUPLE: add <{ ptx-addsub-instruction ptx-float-env } ;
215 ROLE-TUPLE: addc < ptx-addsub-instruction ;
216 ROLE-TUPLE: and < ptx-3op-instruction ;
217 ROLE-TUPLE: atom < ptx-3op-instruction
218 { storage-space maybe{ ptx-storage-space } }
220 { c maybe{ ptx-operand } } ;
221 ROLE-TUPLE: bar.arrive < ptx-instruction
224 ROLE-TUPLE: bar.red < ptx-2op-instruction
226 { b maybe{ ptx-operand } }
228 ROLE-TUPLE: bar.sync < ptx-instruction
230 { b maybe{ ptx-operand } } ;
231 ROLE-TUPLE: bfe < ptx-4op-instruction ;
232 ROLE-TUPLE: bfi < ptx-5op-instruction ;
233 ROLE-TUPLE: bfind < ptx-2op-instruction
234 { shiftamt? boolean } ;
235 ROLE-TUPLE: bra < ptx-branch-instruction ;
236 ROLE-TUPLE: brev < ptx-2op-instruction ;
237 ROLE-TUPLE: brkpt < ptx-instruction ;
238 ROLE-TUPLE: call < ptx-branch-instruction
239 { return maybe{ ptx-operand } }
241 ROLE-TUPLE: clz < ptx-2op-instruction ;
242 ROLE-TUPLE: cnot < ptx-2op-instruction ;
243 ROLE-TUPLE: copysign < ptx-3op-instruction ;
244 ROLE-TUPLE: cos <{ ptx-2op-instruction ptx-float-env } ;
245 ROLE-TUPLE: cvt < ptx-2op-instruction
246 { round maybe{ ptx-rounding-mode } }
249 { dest-type ptx-type } ;
250 ROLE-TUPLE: cvta < ptx-2op-instruction
252 { storage-space maybe{ ptx-storage-space } } ;
253 ROLE-TUPLE: div <{ ptx-3op-instruction ptx-float-env } ;
254 ROLE-TUPLE: ex2 <{ ptx-2op-instruction ptx-float-env } ;
255 ROLE-TUPLE: exit < ptx-instruction ;
256 ROLE-TUPLE: fma <{ ptx-mad-instruction ptx-float-env } ;
257 ROLE-TUPLE: isspacep < ptx-instruction
258 { storage-space ptx-storage-space }
261 ROLE-TUPLE: ld < ptx-ldst-instruction ;
262 ROLE-TUPLE: ldu < ptx-ldst-instruction ;
263 ROLE-TUPLE: lg2 <{ ptx-2op-instruction ptx-float-env } ;
264 ROLE-TUPLE: mad <{ ptx-mad-instruction ptx-float-env } ;
265 ROLE-TUPLE: mad24 < ptx-mad-instruction ;
266 ROLE-TUPLE: max <{ ptx-3op-instruction ptx-float-ftz } ;
267 ROLE-TUPLE: membar < ptx-instruction
268 { level ptx-membar-level } ;
269 ROLE-TUPLE: min <{ ptx-3op-instruction ptx-float-ftz } ;
270 ROLE-TUPLE: mov < ptx-2op-instruction ;
271 ROLE-TUPLE: mul <{ ptx-mul-instruction ptx-float-env } ;
272 ROLE-TUPLE: mul24 < ptx-mul-instruction ;
273 ROLE-TUPLE: neg <{ ptx-2op-instruction ptx-float-ftz } ;
274 ROLE-TUPLE: not < ptx-2op-instruction ;
275 ROLE-TUPLE: or < ptx-3op-instruction ;
276 ROLE-TUPLE: pmevent < ptx-instruction
278 ROLE-TUPLE: popc < ptx-2op-instruction ;
279 ROLE-TUPLE: prefetch < ptx-instruction
281 { storage-space maybe{ ptx-storage-space } }
282 { level ptx-cache-level } ;
283 ROLE-TUPLE: prefetchu < ptx-instruction
285 { level ptx-cache-level } ;
286 ROLE-TUPLE: prmt < ptx-4op-instruction
287 { mode maybe{ ptx-prmt-mode } } ;
288 ROLE-TUPLE: rcp <{ ptx-2op-instruction ptx-float-env } ;
289 ROLE-TUPLE: red < ptx-2op-instruction
290 { storage-space maybe{ ptx-storage-space } }
292 ROLE-TUPLE: rem < ptx-3op-instruction ;
293 ROLE-TUPLE: ret < ptx-instruction ;
294 ROLE-TUPLE: rsqrt <{ ptx-2op-instruction ptx-float-env } ;
295 ROLE-TUPLE: sad < ptx-4op-instruction ;
296 ROLE-TUPLE: selp < ptx-4op-instruction ;
297 ROLE-TUPLE: set < ptx-set-instruction
298 { dest-type ptx-type } ;
299 ROLE-TUPLE: setp < ptx-set-instruction
300 { |dest maybe{ ptx-operand } } ;
301 ROLE-TUPLE: shl < ptx-3op-instruction ;
302 ROLE-TUPLE: shr < ptx-3op-instruction ;
303 ROLE-TUPLE: sin <{ ptx-2op-instruction ptx-float-env } ;
304 ROLE-TUPLE: slct < ptx-4op-instruction
305 { dest-type ptx-type }
307 ROLE-TUPLE: sqrt <{ ptx-2op-instruction ptx-float-env } ;
308 ROLE-TUPLE: st < ptx-ldst-instruction ;
309 ROLE-TUPLE: sub <{ ptx-addsub-instruction ptx-float-env } ;
310 ROLE-TUPLE: subc < ptx-addsub-instruction ;
311 ROLE-TUPLE: suld < ptx-instruction-not-supported-yet ;
312 ROLE-TUPLE: sured < ptx-instruction-not-supported-yet ;
313 ROLE-TUPLE: sust < ptx-instruction-not-supported-yet ;
314 ROLE-TUPLE: suq < ptx-instruction-not-supported-yet ;
315 ROLE-TUPLE: testp < ptx-2op-instruction
316 { op ptx-testp-op } ;
317 ROLE-TUPLE: tex < ptx-instruction-not-supported-yet ;
318 ROLE-TUPLE: txq < ptx-instruction-not-supported-yet ;
319 ROLE-TUPLE: trap < ptx-instruction ;
320 ROLE-TUPLE: vabsdiff < ptx-instruction-not-supported-yet ;
321 ROLE-TUPLE: vadd < ptx-instruction-not-supported-yet ;
322 ROLE-TUPLE: vmad < ptx-instruction-not-supported-yet ;
323 ROLE-TUPLE: vmax < ptx-instruction-not-supported-yet ;
324 ROLE-TUPLE: vmin < ptx-instruction-not-supported-yet ;
325 ROLE-TUPLE: vset < ptx-instruction-not-supported-yet ;
326 ROLE-TUPLE: vshl < ptx-instruction-not-supported-yet ;
327 ROLE-TUPLE: vshr < ptx-instruction-not-supported-yet ;
328 ROLE-TUPLE: vsub < ptx-instruction-not-supported-yet ;
329 ROLE-TUPLE: vote < ptx-2op-instruction
330 { mode ptx-vote-mode } ;
331 ROLE-TUPLE: xor < ptx-3op-instruction ;
333 GENERIC: ptx-element-label ( elt -- label )
334 M: object ptx-element-label drop f ;
336 GENERIC: ptx-semicolon? ( elt -- ? )
337 M: object ptx-semicolon? drop t ;
338 M: ptx-target ptx-semicolon? drop f ;
339 M: ptx-entry ptx-semicolon? drop f ;
340 M: ptx-func ptx-semicolon? drop f ;
341 M: .file ptx-semicolon? drop f ;
342 M: .loc ptx-semicolon? drop f ;
344 GENERIC: write-ptx-operand ( operand -- )
346 M: string write-ptx-operand write ;
347 M: integer write-ptx-operand number>string write ;
348 M: float write-ptx-operand "0d" write double>bits >hex 16 CHAR: 0 pad-head write ;
349 M: ptx-negation write-ptx-operand "!" write var>> write ;
350 M: ptx-vector write-ptx-operand
352 elements>> [ ", " write ] [ write-ptx-operand ] interleave
354 M: ptx-element write-ptx-operand dup var>> write "[" write index>> number>string write "]" write ;
355 M: ptx-indirect write-ptx-operand
357 dup base>> write-ptx-operand
359 { [ dup zero? ] [ drop ] }
360 { [ dup 0 < ] [ number>string write ] }
361 [ "+" write number>string write ]
365 GENERIC: (write-ptx-element) ( elt -- )
367 : write-ptx-element ( elt -- )
368 dup ptx-element-label [ write ":" write ] when*
369 "\t" write dup (write-ptx-element)
370 ptx-semicolon? [ ";" print ] [ nl ] if ;
372 : write-ptx ( ptx -- )
373 "\t.version " write dup version>> print
374 dup target>> write-ptx-element
375 body>> [ write-ptx-element ] each ;
377 : write-ptx-symbol ( symbol/f -- )
378 [ name>> write ] when* ;
380 M: f (write-ptx-element)
383 M: word (write-ptx-element)
386 M: .const (write-ptx-element)
388 bank>> [ "[" write number>string write "]" write ] when* ;
389 M: .v2 (write-ptx-element)
390 ".v2" write of>> (write-ptx-element) ;
391 M: .v4 (write-ptx-element)
392 ".v4" write of>> (write-ptx-element) ;
393 M: .struct (write-ptx-element)
394 ".struct " write name>> write ;
396 M: ptx-target (write-ptx-element)
398 [ arch>> [ name>> ] [ f ] if* ]
399 [ map_f64_to_f32?>> [ "map_f64_to_f32" ] [ f ] if ]
400 [ texmode>> [ name>> ] [ f ] if* ] tri
401 3array sift [ ", " write ] [ write ] interleave ;
403 : write-ptx-dim ( dim -- )
405 { [ dup zero? ] [ drop "[]" write ] }
406 { [ dup sequence? ] [ [ "[" write number>string write "]" write ] each ] }
407 [ "[" write number>string write "]" write ]
410 M: ptx-variable (write-ptx-element)
411 dup extern?>> [ ".extern " write ] when
412 dup visible?>> [ ".visible " write ] when
413 dup align>> [ ".align " write number>string write bl ] when*
414 dup storage-space>> (write-ptx-element) bl
415 dup type>> (write-ptx-element) bl
417 dup parameter>> [ "<" write number>string write ">" write ] when*
418 dup dim>> [ write-ptx-dim ] when*
419 dup initializer>> [ " = " write write ] when*
422 : write-params ( params -- )
423 "(" write unclip (write-ptx-element)
424 [ ", " write (write-ptx-element) ] each
427 : write-body ( params -- )
429 [ write-ptx-element ] each
432 : write-entry ( entry -- )
434 dup params>> [ bl write-params ] when* nl
435 dup directives>> [ (write-ptx-element) nl ] each
436 dup body>> write-body
439 M: ptx-entry (write-ptx-element)
443 M: ptx-func (write-ptx-element)
445 dup return>> [ "(" write (write-ptx-element) ") " write ] when*
448 M: .file (write-ptx-element)
449 ".file " write info>> write ;
450 M: .loc (write-ptx-element)
451 ".loc " write info>> write ;
452 M: .maxnctapersm (write-ptx-element)
453 ".maxnctapersm " write ncta>> number>string write ;
454 M: .minnctapersm (write-ptx-element)
455 ".minnctapersm " write ncta>> number>string write ;
456 M: .maxnreg (write-ptx-element)
457 ".maxnreg " write n>> number>string write ;
458 M: .maxntid (write-ptx-element)
460 dup sequence? [ [ ", " write ] [ number>string write ] interleave ] [ number>string write ] if ;
461 M: .pragma (write-ptx-element)
462 ".pragma \"" write pragma>> write "\"" write ;
464 M: ptx-instruction ptx-element-label
467 : write-insn ( insn name -- insn )
469 [ "@" write write-ptx-operand bl ] when*
472 : write-2op ( insn -- )
473 dup type>> (write-ptx-element) bl
474 dup dest>> write-ptx-operand ", " write
475 dup a>> write-ptx-operand
478 : write-3op ( insn -- )
479 dup write-2op ", " write
480 dup b>> write-ptx-operand
483 : write-4op ( insn -- )
484 dup write-3op ", " write
485 dup c>> write-ptx-operand
488 : write-5op ( insn -- )
489 dup write-4op ", " write
490 dup d>> write-ptx-operand
493 : write-ftz ( insn -- )
494 ftz?>> [ ".ftz" write ] when ;
496 : write-sat ( insn -- )
497 sat?>> [ ".sat" write ] when ;
499 : write-float-env ( insn -- )
500 dup round>> (write-ptx-element)
503 : write-int-addsub ( insn -- )
505 dup cc?>> [ ".cc" write ] when
508 : write-addsub ( insn -- )
512 : write-ldst ( insn -- )
513 dup volatile?>> [ ".volatile" write ] when
514 dup storage-space>> (write-ptx-element)
515 dup cache-op>> (write-ptx-element)
518 : (write-mul) ( insn -- )
519 dup mode>> (write-ptx-element)
522 : write-mul ( insn -- )
527 : write-mad ( insn -- )
533 : write-uni ( insn -- )
534 uni?>> [ ".uni" write ] when ;
536 : write-set ( insn -- )
537 dup cmp-op>> (write-ptx-element)
538 dup bool-op>> (write-ptx-element)
541 M: abs (write-ptx-element)
545 M: add (write-ptx-element)
548 M: addc (write-ptx-element)
551 M: and (write-ptx-element)
554 M: atom (write-ptx-element)
556 dup storage-space>> (write-ptx-element)
557 dup op>> (write-ptx-element)
559 c>> [ ", " write write-ptx-operand ] when* ;
560 M: bar.arrive (write-ptx-element)
561 "bar.arrive " write-insn
562 dup a>> write-ptx-operand ", " write
563 dup b>> write-ptx-operand
565 M: bar.red (write-ptx-element)
567 dup op>> (write-ptx-element)
569 dup b>> [ ", " write write-ptx-operand ] when*
570 ", " write c>> write-ptx-operand ;
571 M: bar.sync (write-ptx-element)
572 "bar.sync " write-insn
573 dup a>> write-ptx-operand
574 dup b>> [ ", " write write-ptx-operand ] when*
576 M: bfe (write-ptx-element)
579 M: bfi (write-ptx-element)
582 M: bfind (write-ptx-element)
584 dup shiftamt?>> [ ".shiftamt" write ] when
586 M: bra (write-ptx-element)
590 M: brev (write-ptx-element)
593 M: brkpt (write-ptx-element)
594 "brkpt" write-insn drop ;
595 M: call (write-ptx-element)
598 dup return>> [ "(" write write-ptx-operand "), " write ] when*
600 dup params>> [ ", (" write [ ", " write ] [ write-ptx-operand ] interleave ")" write ] unless-empty
602 M: clz (write-ptx-element)
605 M: cnot (write-ptx-element)
608 M: copysign (write-ptx-element)
609 "copysign" write-insn
611 M: cos (write-ptx-element)
615 M: cvt (write-ptx-element)
617 dup round>> (write-ptx-element)
620 dup dest-type>> (write-ptx-element)
622 M: cvta (write-ptx-element)
624 dup to?>> [ ".to" write ] when
625 dup storage-space>> (write-ptx-element)
627 M: div (write-ptx-element)
631 M: ex2 (write-ptx-element)
635 M: exit (write-ptx-element)
636 "exit" write-insn drop ;
637 M: fma (write-ptx-element)
640 M: isspacep (write-ptx-element)
641 "isspacep" write-insn
642 dup storage-space>> (write-ptx-element)
644 dup dest>> write-ptx-operand ", " write a>> write-ptx-operand ;
645 M: ld (write-ptx-element)
648 M: ldu (write-ptx-element)
651 M: lg2 (write-ptx-element)
655 M: mad (write-ptx-element)
658 M: mad24 (write-ptx-element)
663 M: max (write-ptx-element)
667 M: membar (write-ptx-element)
669 dup level>> (write-ptx-element)
671 M: min (write-ptx-element)
675 M: mov (write-ptx-element)
678 M: mul (write-ptx-element)
681 M: mul24 (write-ptx-element)
685 M: neg (write-ptx-element)
689 M: not (write-ptx-element)
692 M: or (write-ptx-element)
695 M: pmevent (write-ptx-element)
696 "pmevent" write-insn bl a>> write ;
697 M: popc (write-ptx-element)
700 M: prefetch (write-ptx-element)
701 "prefetch" write-insn
702 dup storage-space>> (write-ptx-element)
703 dup level>> (write-ptx-element)
704 bl a>> write-ptx-operand ;
705 M: prefetchu (write-ptx-element)
706 "prefetchu" write-insn
707 dup level>> (write-ptx-element)
708 bl a>> write-ptx-operand ;
709 M: prmt (write-ptx-element)
711 dup type>> (write-ptx-element)
712 dup mode>> (write-ptx-element) bl
713 dup dest>> write-ptx-operand ", " write
714 dup a>> write-ptx-operand ", " write
715 dup b>> write-ptx-operand ", " write
716 dup c>> write-ptx-operand
718 M: rcp (write-ptx-element)
722 M: red (write-ptx-element)
724 dup storage-space>> (write-ptx-element)
725 dup op>> (write-ptx-element)
727 M: rem (write-ptx-element)
730 M: ret (write-ptx-element)
731 "ret" write-insn drop ;
732 M: rsqrt (write-ptx-element)
736 M: sad (write-ptx-element)
739 M: selp (write-ptx-element)
742 M: set (write-ptx-element)
745 dup dest-type>> (write-ptx-element)
747 c>> [ ", " write write-ptx-operand ] when* ;
748 M: setp (write-ptx-element)
751 dup type>> (write-ptx-element) bl
752 dup dest>> write-ptx-operand
753 dup |dest>> [ "|" write write-ptx-operand ] when* ", " write
754 dup a>> write-ptx-operand ", " write
755 dup b>> write-ptx-operand
756 c>> [ ", " write write-ptx-operand ] when* ;
757 M: shl (write-ptx-element)
760 M: shr (write-ptx-element)
763 M: sin (write-ptx-element)
767 M: slct (write-ptx-element)
770 dup dest-type>> (write-ptx-element)
772 M: sqrt (write-ptx-element)
776 M: st (write-ptx-element)
779 M: sub (write-ptx-element)
782 M: subc (write-ptx-element)
785 M: testp (write-ptx-element)
787 dup op>> (write-ptx-element)
789 M: trap (write-ptx-element)
790 "trap" write-insn drop ;
791 M: vote (write-ptx-element)
793 dup mode>> (write-ptx-element)
795 M: xor (write-ptx-element)
799 : ptx>string ( ptx -- string )
800 [ write-ptx ] with-string-writer ;