|
| 1 | +F:(0#`)!() |
| 2 | +hx8:{[i]"0x",`x@|C i+!8} |
| 3 | +delta:{x-(*x),-1_x} |
| 4 | + |
| 5 | +prg:{ |
| 6 | + c:1_&P=0 |
| 7 | + excl:!0 |
| 8 | + fncs:"\n"/y@&`fun=T c |
| 9 | + cons:"\n"/y@&`con=T c |
| 10 | + vars:"\n"/y@&`var=T c |
| 11 | + data:$[#D;"(data (i32.const 0) \"",(wadat D),"\")";""] |
| 12 | + tabd:$[0<i:1+|/I c t:&`tab=T c;"(table (export \"table\") ",($i)," funcref)";""] |
| 13 | + tabl:watbl y t |
| 14 | + mems:"(memory (export \"memory\") 1)" |
| 15 | + "\n"/(""~/)_("(module";mems;cons;vars;data;fncs;tabd;tabl;")\n")} |
| 16 | + |
| 17 | +wadat:{,/@["\\",/256^`x@_!256;0+wac;wac:_("a"+!26),("A"+!26),("0"+!10),"~!@#$%^&*()_+{}[]|;:<>,./?"]x} |
| 18 | +mem:{y;""} |
| 19 | +con:{"(global $",($S x)," ",(type S 1+x)," (",(*y),"))"} |
| 20 | +var:{"(global $",($S x)," (mut ",(type S 1+x),") (",(*y),"))"} |
| 21 | +tab:{y;(I x;S x)} |
| 22 | +watbl:{"\n"/{"(elem (i32.const ",($x),") func ",y,")"}'[(*x)i;" "/'"$",''$(i:&~1=delta@*x)^(x:+x)1]} |
| 23 | +fun:{s:$S x |
| 24 | + args:" "/y@&`arg=T c:&x=P |
| 25 | + rets:" "/y@&`res=T c |
| 26 | + locs:" "/y@&`loc=T c |
| 27 | + body: y@*&`ast=T c |
| 28 | + expo:$[I x;"(export \"",s,"\")";""] |
| 29 | + (" "/(""~/)_("(func";"$",s;expo;args;rets;locs)),"\n",body,")"} |
| 30 | +arg:{" "/(""~/)_("(param";(y 0);(type S x),")")} |
| 31 | +sym:{y;"$",$S x} |
| 32 | +res:{y;"(result ",(type S x),")"} |
| 33 | +loc:{"(local ",(*y)," ",(type S x),")"} |
| 34 | +stm:{"\n"/y} |
| 35 | +ast:stm |
| 36 | +lod:{(y 0),"\n",$[`b~s:S x;"i32.load8_s";(type s),".load"]} |
| 37 | +sto:{"\n"/y,,$[`b~s:S x;"i32.store8";(type s),".store"]} |
| 38 | +ant:{(y,'"\n"),t,".const -1\n",t,".xor\n",(t:type S x),".and"} |
| 39 | +asn:{ y,("\nlocal.set $";"\nglobal.set $")[I x],$S x } |
| 40 | +
|
| 41 | +cal:{$[`panic~s:S x;"unreachable";("\n"/y),"\n",$[#c:inst s;c;"call $",$s]]} |
| 42 | +
|
| 43 | +cli:{("\n"/((I x)#1_y),,*y),"\n","call_indirect "," "/((1+I x)_y),$[S x;,"(result ",(type S x),")";()]} |
| 44 | +dfr:{y;""} |
| 45 | +drp:{"\n"/y,,"drop"} |
| 46 | +cst:{t:!type;(y 1),"\n",cast i:(t?y 0)+5*t?S x} |
| 47 | +for:{$[I x;slp[x;y];lop[x;y]]} |
| 48 | +slp:{"\n"/("loop";-5_"\n"/2_y;y 1;*y;"br_if 0\nend")} |
| 49 | +lop:{bl:("block";"loop"),'$[`~S x;2^"";(l,"1";(l:" $",$S x),"0")] |
| 50 | + "\n"/bl,($[#*y;(*y),"\ni32.eqz\nbr_if 1";""]; "\n"/2_y;y 1;"br 0\nend\nend")} |
| 51 | +get:{y;"local.get $",$S x} |
| 52 | +Get:{y;"global.get $",$S x} |
| 53 | +jmp:{y;"br ",$[`~S x;$1+I x;"$",($S x),$I x]} |
| 54 | + |
| 55 | +flts:(,0x182d4454fb210940)!,"3.141592653589793" /predefined constants that don't roundtrip |
| 56 | +flt:{x:C x+!8;$[#r:flts x;r;$*`f x]} |
| 57 | +lit:{y;i:I x;(type t),".const ",$[`f~t:S x;flt i |
| 58 | + `i~t;$i |
| 59 | + `u~t;$[i<0;"0x",`x@|`c@,i;$i] |
| 60 | + $[i~0;,"0";(-1~h:*`i C 4+i+!4)&0>j:*`i C i+!4; $j; (h~0)&j>0; $j; hx8 i]]} |
| 61 | +
|
| 62 | +neg:{$[`f~t:S x;(*y),"\nf64.neg";"\n"/(t,".const 0";(*y);(t:type t),".sub")]} |
| 63 | +not:{(*y),"\ni32.eqz"} |
| 64 | +nop:{y;""} |
| 65 | +ret:{("\n"/y),"\nreturn"} |
| 66 | +swc:{ |
| 67 | + r:(,"block")@&n:#$[I x;y;y,:,""] |
| 68 | + r[0],:$[S x;" (result ",(type S x),")";""] |
| 69 | + r,:,*y |
| 70 | + r,:,"br_table ",(" "/$!n-1),"\nend" |
| 71 | + r,:(1_y),'"\nbr ",/($|!n-1),\"\nend" |
| 72 | + ("\n"/r)} |
| 73 | +cnd:{"\n"/(*y;$[#t:type S x;"if (result ",t,")";"if"];y 1;$[3~#y;"else\n",(y 2);""],"\nend")} |
| 74 | +typ:{y;S x} /keep as symbol |
| 75 | + |
| 76 | +wa2: {"\n"/(z 0;z 1;(type S y),x)} |
| 77 | +wa2s:{"\n"/(z 0;z 1;(type t ),x,sign@t:S y)} |
| 78 | +add:wa2".add";sub:wa2".sub" |
| 79 | +mul:wa2".mul";div:wa2s".div";mod:wa2s".rem" |
| 80 | +and:wa2".and";bnd:wa2".and" |
| 81 | +neq:wa2".ne" |
| 82 | +mor:wa2s".gt";gte:wa2s".ge";les:wa2s".lt";lte:wa2s".le"; |
| 83 | +orr:wa2".or";bor:orr;xor:wa2".xor" |
| 84 | +eql:wa2".eq" |
| 85 | +shl:wa2".shl";shr:wa2s".shr" |
| 86 | + |
| 87 | +type:`i`u`j`k`f!("i32";"i32";"i64";"i64";"f64") |
| 88 | +sign:`i`u`j`k`f!("_s"; "_u"; "_s"; "_u"; "") |
| 89 | +inst:`I32B`Memorysize`Memorygrow`Memorycopy`Memoryfill!("nop";"memory.size";"memory.grow";"memory.copy";"memory.fill") |
| 90 | +inst,:`Memorysize2`Memorygrow2`Memorycopy2`Memorycopy3!((,"unreachable\ni32.const 0")0 0),(,"drop\ndrop\ndrop\nunreachable")0 0 |
| 91 | +inst,:{x!{(_32+*x),(1_3#x),".",3_x}'$x}`I32clz`F64abs`F64sqrt`F64floor`F64copysign`F64min`F64max`"F64reinterpret_i64"`"I64reinterpret_f64" |
| 92 | + |
| 93 | +/ i u j k f |
| 94 | +cast:("" ;"" ;"i32.wrap_i64" ;"i32.wrap_i64" ;"i32.trunc_f64_s" /i |
| 95 | + "" ;"" ;"i32.wrap_i64" ;"i32.wrap_i64" ;"i32.trunc_f64_u" /u |
| 96 | + "i64.extend_i32_s" ;"" ;"" ;"" ;"i64.trunc_f64_s" /j |
| 97 | + "i64.extend_i32_u" ;"i64.extend_i32_u" ;"" ;"" ;"i64.trunc_f64_u" /k |
| 98 | + "f64.convert_i32_s";"f64.convert_i32_u";"f64.convert_i64_s";"f64.convert_i64_u";"") /f |
| 99 | + |
| 100 | + |
| 101 | +F[`prg`mem`con`var`tab`fun`arg`sym`res`loc`ast`lod`sto]:(prg;mem;con;var;tab;fun;arg;sym;res;loc;ast;lod;sto) |
| 102 | +F[`add`and`bnd`ant`asn`cal`cli`cnd`dfr`div`drp`eql`cst]:(add;and;bnd;ant;asn;cal;cli;cnd;dfr;div;drp;eql;cst) |
| 103 | +F[`for`get`Get`gte`jmp`les`lit`lte`mod`mor`mul`neg ]:(for;get;Get;gte;jmp;les;lit;lte;mod;mor;mul;neg) |
| 104 | +F[`neq`nop`not`orr`bor`xor`ret`shl`shr`stm`sub`swc`typ]:(neq;nop;not;orr;bor;xor;ret;shl;shr;stm;sub;swc;typ) |
| 105 | + |
| 106 | +n:{x[y]+:1}/(,&#P),P;n[0]-:1 |
| 107 | +s:{c:(0,(#x)-z)^x;(c 0),,,/(F T y)[y;|c 1]}/[();|!#P;|n] |
| 108 | +`<*s |
0 commit comments