Skip to content

Instantly share code, notes, and snippets.

@paniq
Last active December 11, 2020 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paniq/147289480c9ebff8eae9010c8dc964f2 to your computer and use it in GitHub Desktop.
Save paniq/147289480c9ebff8eae9010c8dc964f2 to your computer and use it in GitHub Desktop.
using import enum
using import struct
using import Rc
using import Array
using import Map
using import testing
let Id = u32
let NoId = (Id 0)
let NoType = NoId
enum Op : u32
Nop = 0
# Op.TypeInt NoType <width : i32> <signed : bool>
TypeInt
# Op.ConstInt <type : Id> <value : u32>
ConstInt
struct Node
typeId : Id
opCode : Op
operands : (Array Id)
fn add (self operand)
'append self.operands operand
;
fn __hash (self)
h := (hash self.typeId self.opCode)
fold (h = h) for operand in self.operands
hash h operand
@@ memo
inline __== (cls T)
static-if (cls == T)
fn (a b)
and
a.opCode == b.opCode
a.typeId == b.typeId
(countof a.operands) == (countof b.operands)
do
for u v in (zip a.operands b.operands)
if (u != v)
break false
else true
inline __typecall (cls typeId opCode ...)
super-type.__typecall cls typeId opCode
static-if (va-empty? ...) ()
else
local ops : (Array Id)
va-map
inline (value)
'append ops value
...
ops
let RcNode = (Rc Node)
struct UVMIRBuilder
uniqueId : Id = 0
nodes : (Map Id RcNode)
rnodes :
Map RcNode Id
fn (rcnode)
hash (rcnode as Node)
fn uniqueId (self)
self.uniqueId += 1
copy self.uniqueId
fn... nodeId (self, node : Node)
let id = ('getdefault self.rnodes node NoId)
if (id == NoId)
let node = (Rc.wrap (deref node))
let newid = ('uniqueId self)
'set self.nodes newid (copy node)
'set self.rnodes node newid
newid
else id
fn... integerType (self, width : i32, signed : bool)
'nodeId self
Node NoType Op.TypeInt
width as u32; signed as u32
fn... constInt (self, typeId : Id, value : u32)
'nodeId self
Node typeId Op.ConstInt typeId value
local builder : UVMIRBuilder
test
==
'integerType builder 32 false
'integerType builder 32 false
test
!=
'integerType builder 16 false
'integerType builder 32 false
test
==
'constInt builder
'integerType builder 32 false
123
'constInt builder
'integerType builder 32 false
123
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment