1 ! Copyright (C) 2010 Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays combinators io io.streams.string kernel
4 math math.parser roles sequences strings variants words ;
7 UNION: dim integer sequence ;
15 .texref .samplerref .surfref
16 .v2: { { of ptx-type } }
17 .v4: { { of ptx-type } }
18 .struct: { { name string } } ;
21 sm_10 sm_11 sm_12 sm_13 sm_20 ;
24 .texmode_unified .texmode_independent ;
26 VARIANT: ptx-storage-space
29 .const: { { bank maybe{ integer } } }
36 ROLE-TUPLE: ptx-target
37 { arch maybe{ ptx-arch } }
38 { map_f64_to_f32? boolean }
39 { texmode maybe{ ptx-texmode } } ;
46 ROLE-TUPLE: ptx-struct-definition
50 ROLE-TUPLE: ptx-variable
53 { align maybe{ integer } }
54 { storage-space ptx-storage-space }
57 { parameter maybe{ integer } }
59 { initializer maybe{ string } } ;
61 ROLE-TUPLE: ptx-negation
64 ROLE-TUPLE: ptx-vector
67 ROLE-TUPLE: ptx-element
74 ROLE-TUPLE: ptx-indirect
79 integer float ptx-var ptx-negation ptx-vector ptx-indirect ;
81 ROLE-TUPLE: ptx-instruction
82 { label maybe{ string } }
83 { predicate maybe{ ptx-operand } } ;
91 ROLE-TUPLE: ptx-func < ptx-entry
92 { return maybe{ ptx-variable } } ;
94 ROLE-TUPLE: ptx-directive ;
96 ROLE-TUPLE: .file < ptx-directive
98 ROLE-TUPLE: .loc < ptx-directive
100 ROLE-TUPLE: .maxnctapersm < ptx-directive
102 ROLE-TUPLE: .minnctapersm < ptx-directive
104 ROLE-TUPLE: .maxnreg < ptx-directive
106 ROLE-TUPLE: .maxntid < ptx-directive
108 ROLE-TUPLE: .pragma < ptx-directive
111 VARIANT: ptx-float-rounding-mode
112 .rn .rz .rm .rp .approx .full ;
113 VARIANT: ptx-int-rounding-mode
114 .rni .rzi .rmi .rpi ;
116 UNION: ptx-rounding-mode
117 ptx-float-rounding-mode ptx-int-rounding-mode ;
119 ROLE-TUPLE: ptx-typed-instruction < ptx-instruction
121 { dest ptx-operand } ;
123 ROLE-TUPLE: ptx-2op-instruction < ptx-typed-instruction
126 ROLE-TUPLE: ptx-3op-instruction < ptx-typed-instruction
130 ROLE-TUPLE: ptx-4op-instruction < ptx-typed-instruction
135 ROLE-TUPLE: ptx-5op-instruction < ptx-typed-instruction
141 ROLE-TUPLE: ptx-addsub-instruction < ptx-3op-instruction
145 VARIANT: ptx-mul-mode
148 ROLE-TUPLE: ptx-mul-instruction < ptx-3op-instruction
149 { mode maybe{ ptx-mul-mode } } ;
151 ROLE-TUPLE: ptx-mad-instruction < ptx-4op-instruction
152 { mode maybe{ ptx-mul-mode } }
155 VARIANT: ptx-prmt-mode
156 .f4e .b4e .rc8 .ecl .ecr .rc16 ;
160 ROLE: ptx-float-env < ptx-float-ftz
161 { round maybe{ ptx-float-rounding-mode } } ;
163 VARIANT: ptx-testp-op
164 .finite .infinite .number .notanumber .normal .subnormal ;
175 .and .or .xor .cas .exch .add .inc .dec .min .max
178 SINGLETONS: .lo .hi ;
179 INSTANCE: .lo ptx-mul-mode
180 INSTANCE: .lo ptx-cmp-op
181 INSTANCE: .hi ptx-mul-mode
182 INSTANCE: .hi ptx-cmp-op
184 ROLE-TUPLE: ptx-set-instruction < ptx-3op-instruction
185 { cmp-op ptx-cmp-op }
186 { bool-op maybe{ ptx-op } }
187 { c maybe{ ptx-operand } }
190 VARIANT: ptx-cache-op
194 ROLE-TUPLE: ptx-ldst-instruction < ptx-2op-instruction
195 { volatile? boolean }
196 { storage-space maybe{ ptx-storage-space } }
197 { cache-op maybe{ ptx-cache-op } } ;
199 VARIANT: ptx-cache-level
202 ROLE-TUPLE: ptx-branch-instruction < ptx-instruction
206 VARIANT: ptx-membar-level
209 VARIANT: ptx-vote-mode
210 .all .any .uni .ballot ;
212 ROLE-TUPLE: ptx-instruction-not-supported-yet < ptx-instruction ;
214 ROLE-TUPLE: abs <{ ptx-2op-instruction ptx-float-ftz } ;
215 ROLE-TUPLE: add <{ ptx-addsub-instruction ptx-float-env } ;
216 ROLE-TUPLE: addc < ptx-addsub-instruction ;
217 ROLE-TUPLE: and < ptx-3op-instruction ;
218 ROLE-TUPLE: atom < ptx-3op-instruction
219 { storage-space maybe{ ptx-storage-space } }
221 { c maybe{ ptx-operand } } ;
222 ROLE-TUPLE: bar.arrive < ptx-instruction
225 ROLE-TUPLE: bar.red < ptx-2op-instruction
227 { b maybe{ ptx-operand } }
229 ROLE-TUPLE: bar.sync < ptx-instruction
231 { b maybe{ ptx-operand } } ;
232 ROLE-TUPLE: bfe < ptx-4op-instruction ;
233 ROLE-TUPLE: bfi < ptx-5op-instruction ;
234 ROLE-TUPLE: bfind < ptx-2op-instruction
235 { shiftamt? boolean } ;
236 ROLE-TUPLE: bra < ptx-branch-instruction ;
237 ROLE-TUPLE: brev < ptx-2op-instruction ;
238 ROLE-TUPLE: brkpt < ptx-instruction ;
239 ROLE-TUPLE: call < ptx-branch-instruction
240 { return maybe{ ptx-operand } }
242 ROLE-TUPLE: clz < ptx-2op-instruction ;
243 ROLE-TUPLE: cnot < ptx-2op-instruction ;
244 ROLE-TUPLE: copysign < ptx-3op-instruction ;
245 ROLE-TUPLE: cos <{ ptx-2op-instruction ptx-float-env } ;
246 ROLE-TUPLE: cvt < ptx-2op-instruction
247 { round maybe{ ptx-rounding-mode } }
250 { dest-type ptx-type } ;
251 ROLE-TUPLE: cvta < ptx-2op-instruction
253 { storage-space maybe{ ptx-storage-space } } ;
254 ROLE-TUPLE: div <{ ptx-3op-instruction ptx-float-env } ;
255 ROLE-TUPLE: ex2 <{ ptx-2op-instruction ptx-float-env } ;
256 ROLE-TUPLE: exit < ptx-instruction ;
257 ROLE-TUPLE: fma <{ ptx-mad-instruction ptx-float-env } ;
258 ROLE-TUPLE: isspacep < ptx-instruction
259 { storage-space ptx-storage-space }
262 ROLE-TUPLE: ld < ptx-ldst-instruction ;
263 ROLE-TUPLE: ldu < ptx-ldst-instruction ;
264 ROLE-TUPLE: lg2 <{ ptx-2op-instruction ptx-float-env } ;
265 ROLE-TUPLE: mad <{ ptx-mad-instruction ptx-float-env } ;
266 ROLE-TUPLE: mad24 < ptx-mad-instruction ;
267 ROLE-TUPLE: max <{ ptx-3op-instruction ptx-float-ftz } ;
268 ROLE-TUPLE: membar < ptx-instruction
269 { level ptx-membar-level } ;
270 ROLE-TUPLE: min <{ ptx-3op-instruction ptx-float-ftz } ;
271 ROLE-TUPLE: mov < ptx-2op-instruction ;
272 ROLE-TUPLE: mul <{ ptx-mul-instruction ptx-float-env } ;
273 ROLE-TUPLE: mul24 < ptx-mul-instruction ;
274 ROLE-TUPLE: neg <{ ptx-2op-instruction ptx-float-ftz } ;
275 ROLE-TUPLE: not < ptx-2op-instruction ;
276 ROLE-TUPLE: or < ptx-3op-instruction ;
277 ROLE-TUPLE: pmevent < ptx-instruction
279 ROLE-TUPLE: popc < ptx-2op-instruction ;
280 ROLE-TUPLE: prefetch < ptx-instruction
282 { storage-space maybe{ ptx-storage-space } }
283 { level ptx-cache-level } ;
284 ROLE-TUPLE: prefetchu < ptx-instruction
286 { level ptx-cache-level } ;
287 ROLE-TUPLE: prmt < ptx-4op-instruction
288 { mode maybe{ ptx-prmt-mode } } ;
289 ROLE-TUPLE: rcp <{ ptx-2op-instruction ptx-float-env } ;
290 ROLE-TUPLE: red < ptx-2op-instruction
291 { storage-space maybe{ ptx-storage-space } }
293 ROLE-TUPLE: rem < ptx-3op-instruction ;
294 ROLE-TUPLE: ret < ptx-instruction ;
295 ROLE-TUPLE: rsqrt <{ ptx-2op-instruction ptx-float-env } ;
296 ROLE-TUPLE: sad < ptx-4op-instruction ;
297 ROLE-TUPLE: selp < ptx-4op-instruction ;
298 ROLE-TUPLE: set < ptx-set-instruction
299 { dest-type ptx-type } ;
300 ROLE-TUPLE: setp < ptx-set-instruction
301 { |dest maybe{ ptx-operand } } ;
302 ROLE-TUPLE: shl < ptx-3op-instruction ;
303 ROLE-TUPLE: shr < ptx-3op-instruction ;
304 ROLE-TUPLE: sin <{ ptx-2op-instruction ptx-float-env } ;
305 ROLE-TUPLE: slct < ptx-4op-instruction
306 { dest-type ptx-type }
308 ROLE-TUPLE: sqrt <{ ptx-2op-instruction ptx-float-env } ;
309 ROLE-TUPLE: st < ptx-ldst-instruction ;
310 ROLE-TUPLE: sub <{ ptx-addsub-instruction ptx-float-env } ;
311 ROLE-TUPLE: subc < ptx-addsub-instruction ;
312 ROLE-TUPLE: suld < ptx-instruction-not-supported-yet ;
313 ROLE-TUPLE: sured < ptx-instruction-not-supported-yet ;
314 ROLE-TUPLE: sust < ptx-instruction-not-supported-yet ;
315 ROLE-TUPLE: suq < ptx-instruction-not-supported-yet ;
316 ROLE-TUPLE: testp < ptx-2op-instruction
317 { op ptx-testp-op } ;
318 ROLE-TUPLE: tex < ptx-instruction-not-supported-yet ;
319 ROLE-TUPLE: txq < ptx-instruction-not-supported-yet ;
320 ROLE-TUPLE: trap < ptx-instruction ;
321 ROLE-TUPLE: vabsdiff < ptx-instruction-not-supported-yet ;
322 ROLE-TUPLE: vadd < ptx-instruction-not-supported-yet ;
323 ROLE-TUPLE: vmad < ptx-instruction-not-supported-yet ;
324 ROLE-TUPLE: vmax < ptx-instruction-not-supported-yet ;
325 ROLE-TUPLE: vmin < ptx-instruction-not-supported-yet ;
326 ROLE-TUPLE: vset < ptx-instruction-not-supported-yet ;
327 ROLE-TUPLE: vshl < ptx-instruction-not-supported-yet ;
328 ROLE-TUPLE: vshr < ptx-instruction-not-supported-yet ;
329 ROLE-TUPLE: vsub < ptx-instruction-not-supported-yet ;
330 ROLE-TUPLE: vote < ptx-2op-instruction
331 { mode ptx-vote-mode } ;
332 ROLE-TUPLE: xor < ptx-3op-instruction ;
334 GENERIC: ptx-element-label ( elt -- label )
335 M: object ptx-element-label drop f ;
337 GENERIC: ptx-semicolon? ( elt -- ? )
338 M: object ptx-semicolon? drop t ;
339 M: ptx-target ptx-semicolon? drop f ;
340 M: ptx-entry ptx-semicolon? drop f ;
341 M: ptx-func ptx-semicolon? drop f ;
342 M: .file ptx-semicolon? drop f ;
343 M: .loc ptx-semicolon? drop f ;
345 GENERIC: write-ptx-operand ( operand -- )
347 M: string write-ptx-operand write ;
348 M: integer write-ptx-operand number>string write ;
349 M: float write-ptx-operand "0d" write double>bits >hex 16 CHAR: 0 pad-head write ;
350 M: ptx-negation write-ptx-operand "!" write var>> write ;
351 M: ptx-vector write-ptx-operand
353 elements>> [ ", " write ] [ write-ptx-operand ] interleave
355 M: ptx-element write-ptx-operand dup var>> write "[" write index>> number>string write "]" write ;
356 M: ptx-indirect write-ptx-operand
358 dup base>> write-ptx-operand
360 { [ dup zero? ] [ drop ] }
361 { [ dup 0 < ] [ number>string write ] }
362 [ "+" write number>string write ]
366 GENERIC: (write-ptx-element) ( elt -- )
368 : write-ptx-element ( elt -- )
369 dup ptx-element-label [ write ":" write ] when*
370 "\t" write dup (write-ptx-element)
371 ptx-semicolon? [ ";" print ] [ nl ] if ;
373 : write-ptx ( ptx -- )
374 "\t.version " write dup version>> print
375 dup target>> write-ptx-element
376 body>> [ write-ptx-element ] each ;
378 : write-ptx-symbol ( symbol/f -- )
379 [ name>> write ] when* ;
381 M: f (write-ptx-element)
384 M: word (write-ptx-element)
387 M: .const (write-ptx-element)
389 bank>> [ "[" write number>string write "]" write ] when* ;
390 M: .v2 (write-ptx-element)
391 ".v2" write of>> (write-ptx-element) ;
392 M: .v4 (write-ptx-element)
393 ".v4" write of>> (write-ptx-element) ;
394 M: .struct (write-ptx-element)
395 ".struct " write name>> write ;
397 M: ptx-target (write-ptx-element)
399 [ arch>> [ name>> ] [ f ] if* ]
400 [ map_f64_to_f32?>> [ "map_f64_to_f32" ] [ f ] if ]
401 [ texmode>> [ name>> ] [ f ] if* ] tri
402 3array sift [ ", " write ] [ write ] interleave ;
404 : write-ptx-dim ( dim -- )
406 { [ dup zero? ] [ drop "[]" write ] }
407 { [ dup sequence? ] [ [ "[" write number>string write "]" write ] each ] }
408 [ "[" write number>string write "]" write ]
411 M: ptx-variable (write-ptx-element)
412 dup extern?>> [ ".extern " write ] when
413 dup visible?>> [ ".visible " write ] when
414 dup align>> [ ".align " write number>string write bl ] when*
415 dup storage-space>> (write-ptx-element) bl
416 dup type>> (write-ptx-element) bl
418 dup parameter>> [ "<" write number>string write ">" write ] when*
419 dup dim>> [ write-ptx-dim ] when*
420 dup initializer>> [ " = " write write ] when*
423 : write-params ( params -- )
424 "(" write unclip (write-ptx-element)
425 [ ", " write (write-ptx-element) ] each
428 : write-body ( params -- )
430 [ write-ptx-element ] each
433 : write-entry ( entry -- )
435 dup params>> [ bl write-params ] when* nl
436 dup directives>> [ (write-ptx-element) nl ] each
437 dup body>> write-body
440 M: ptx-entry (write-ptx-element)
444 M: ptx-func (write-ptx-element)
446 dup return>> [ "(" write (write-ptx-element) ") " write ] when*
449 M: .file (write-ptx-element)
450 ".file " write info>> write ;
451 M: .loc (write-ptx-element)
452 ".loc " write info>> write ;
453 M: .maxnctapersm (write-ptx-element)
454 ".maxnctapersm " write ncta>> number>string write ;
455 M: .minnctapersm (write-ptx-element)
456 ".minnctapersm " write ncta>> number>string write ;
457 M: .maxnreg (write-ptx-element)
458 ".maxnreg " write n>> number>string write ;
459 M: .maxntid (write-ptx-element)
461 dup sequence? [ [ ", " write ] [ number>string write ] interleave ] [ number>string write ] if ;
462 M: .pragma (write-ptx-element)
463 ".pragma \"" write pragma>> write "\"" write ;
465 M: ptx-instruction ptx-element-label
468 : write-insn ( insn name -- insn )
470 [ "@" write write-ptx-operand bl ] when*
473 : write-2op ( insn -- )
474 dup type>> (write-ptx-element) bl
475 dup dest>> write-ptx-operand ", " write
476 dup a>> write-ptx-operand
479 : write-3op ( insn -- )
480 dup write-2op ", " write
481 dup b>> write-ptx-operand
484 : write-4op ( insn -- )
485 dup write-3op ", " write
486 dup c>> write-ptx-operand
489 : write-5op ( insn -- )
490 dup write-4op ", " write
491 dup d>> write-ptx-operand
494 : write-ftz ( insn -- )
495 ftz?>> [ ".ftz" write ] when ;
497 : write-sat ( insn -- )
498 sat?>> [ ".sat" write ] when ;
500 : write-float-env ( insn -- )
501 dup round>> (write-ptx-element)
504 : write-int-addsub ( insn -- )
506 dup cc?>> [ ".cc" write ] when
509 : write-addsub ( insn -- )
513 : write-ldst ( insn -- )
514 dup volatile?>> [ ".volatile" write ] when
515 dup storage-space>> (write-ptx-element)
516 dup cache-op>> (write-ptx-element)
519 : (write-mul) ( insn -- )
520 dup mode>> (write-ptx-element)
523 : write-mul ( insn -- )
528 : write-mad ( insn -- )
534 : write-uni ( insn -- )
535 uni?>> [ ".uni" write ] when ;
537 : write-set ( insn -- )
538 dup cmp-op>> (write-ptx-element)
539 dup bool-op>> (write-ptx-element)
542 M: abs (write-ptx-element)
546 M: add (write-ptx-element)
549 M: addc (write-ptx-element)
552 M: and (write-ptx-element)
555 M: atom (write-ptx-element)
557 dup storage-space>> (write-ptx-element)
558 dup op>> (write-ptx-element)
560 c>> [ ", " write write-ptx-operand ] when* ;
561 M: bar.arrive (write-ptx-element)
562 "bar.arrive " write-insn
563 dup a>> write-ptx-operand ", " write
564 dup b>> write-ptx-operand
566 M: bar.red (write-ptx-element)
568 dup op>> (write-ptx-element)
570 dup b>> [ ", " write write-ptx-operand ] when*
571 ", " write c>> write-ptx-operand ;
572 M: bar.sync (write-ptx-element)
573 "bar.sync " write-insn
574 dup a>> write-ptx-operand
575 dup b>> [ ", " write write-ptx-operand ] when*
577 M: bfe (write-ptx-element)
580 M: bfi (write-ptx-element)
583 M: bfind (write-ptx-element)
585 dup shiftamt?>> [ ".shiftamt" write ] when
587 M: bra (write-ptx-element)
591 M: brev (write-ptx-element)
594 M: brkpt (write-ptx-element)
595 "brkpt" write-insn drop ;
596 M: call (write-ptx-element)
599 dup return>> [ "(" write write-ptx-operand "), " write ] when*
601 dup params>> [ ", (" write [ ", " write ] [ write-ptx-operand ] interleave ")" write ] unless-empty
603 M: clz (write-ptx-element)
606 M: cnot (write-ptx-element)
609 M: copysign (write-ptx-element)
610 "copysign" write-insn
612 M: cos (write-ptx-element)
616 M: cvt (write-ptx-element)
618 dup round>> (write-ptx-element)
621 dup dest-type>> (write-ptx-element)
623 M: cvta (write-ptx-element)
625 dup to?>> [ ".to" write ] when
626 dup storage-space>> (write-ptx-element)
628 M: div (write-ptx-element)
632 M: ex2 (write-ptx-element)
636 M: exit (write-ptx-element)
637 "exit" write-insn drop ;
638 M: fma (write-ptx-element)
641 M: isspacep (write-ptx-element)
642 "isspacep" write-insn
643 dup storage-space>> (write-ptx-element)
645 dup dest>> write-ptx-operand ", " write a>> write-ptx-operand ;
646 M: ld (write-ptx-element)
649 M: ldu (write-ptx-element)
652 M: lg2 (write-ptx-element)
656 M: mad (write-ptx-element)
659 M: mad24 (write-ptx-element)
664 M: max (write-ptx-element)
668 M: membar (write-ptx-element)
670 dup level>> (write-ptx-element)
672 M: min (write-ptx-element)
676 M: mov (write-ptx-element)
679 M: mul (write-ptx-element)
682 M: mul24 (write-ptx-element)
686 M: neg (write-ptx-element)
690 M: not (write-ptx-element)
693 M: or (write-ptx-element)
696 M: pmevent (write-ptx-element)
697 "pmevent" write-insn bl a>> write ;
698 M: popc (write-ptx-element)
701 M: prefetch (write-ptx-element)
702 "prefetch" write-insn
703 dup storage-space>> (write-ptx-element)
704 dup level>> (write-ptx-element)
705 bl a>> write-ptx-operand ;
706 M: prefetchu (write-ptx-element)
707 "prefetchu" write-insn
708 dup level>> (write-ptx-element)
709 bl a>> write-ptx-operand ;
710 M: prmt (write-ptx-element)
712 dup type>> (write-ptx-element)
713 dup mode>> (write-ptx-element) bl
714 dup dest>> write-ptx-operand ", " write
715 dup a>> write-ptx-operand ", " write
716 dup b>> write-ptx-operand ", " write
717 dup c>> write-ptx-operand
719 M: rcp (write-ptx-element)
723 M: red (write-ptx-element)
725 dup storage-space>> (write-ptx-element)
726 dup op>> (write-ptx-element)
728 M: rem (write-ptx-element)
731 M: ret (write-ptx-element)
732 "ret" write-insn drop ;
733 M: rsqrt (write-ptx-element)
737 M: sad (write-ptx-element)
740 M: selp (write-ptx-element)
743 M: set (write-ptx-element)
746 dup dest-type>> (write-ptx-element)
748 c>> [ ", " write write-ptx-operand ] when* ;
749 M: setp (write-ptx-element)
752 dup type>> (write-ptx-element) bl
753 dup dest>> write-ptx-operand
754 dup |dest>> [ "|" write write-ptx-operand ] when* ", " write
755 dup a>> write-ptx-operand ", " write
756 dup b>> write-ptx-operand
757 c>> [ ", " write write-ptx-operand ] when* ;
758 M: shl (write-ptx-element)
761 M: shr (write-ptx-element)
764 M: sin (write-ptx-element)
768 M: slct (write-ptx-element)
771 dup dest-type>> (write-ptx-element)
773 M: sqrt (write-ptx-element)
777 M: st (write-ptx-element)
780 M: sub (write-ptx-element)
783 M: subc (write-ptx-element)
786 M: testp (write-ptx-element)
788 dup op>> (write-ptx-element)
790 M: trap (write-ptx-element)
791 "trap" write-insn drop ;
792 M: vote (write-ptx-element)
794 dup mode>> (write-ptx-element)
796 M: xor (write-ptx-element)
800 : ptx>string ( ptx -- string )
801 [ write-ptx ] with-string-writer ;