Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 107 additions & 10 deletions ir.v
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub mut:
refs []IRRef
refs_map map[RID]VID
vars_map map[string]VID
operand []IROperand
consts []IRConstant
entrybb BBID
is_inline bool // Later
inline_defaults []int // Later
Expand Down Expand Up @@ -74,6 +74,8 @@ pub mut:
stmts []Stmt // The list of statements from the IR that form this basic block
}

pub type BBAID = int

pub struct IRBasicBlockArg {
pub mut:
name string
Expand All @@ -83,7 +85,12 @@ pub mut:
}

type IID = int
pub type IRInstruction = IRMacroLiteralCmd | IRDefine | IRTypedDefine | IRBinaryOp | IRUnaryOp
pub type IRInstruction = IRMacroLiteralCmd
| IRDefine
| IRTypedDefine
| IRBinaryOp
| IRUnaryOp
| IRAssign

pub struct IRDefine {
pub mut:
Expand All @@ -98,14 +105,55 @@ pub mut:
result RID
}

pub fn (t TokenKind) to_assign_op() AssignOp {
return match t {
.assign { .assign }
.plus_assign { .add_assign }
.minus_assign { .sub_assign }
.star_assign { .mul_assign }
.slash_assign { .div_assign }
.percent_assign { .mod_assign }
.swap { .swap }
else { panic('illegal use of token ${t} as a Assign operation') }
}
}

pub enum AssignOp {
assign
add
sub
mul
div
mod
add_assign
sub_assign
mul_assign
div_assign
mod_assign
swap
}

pub fn (a AssignOp) print() {
match a {
.assign {
print('=')
}
.add_assign {
print('+=')
}
.sub_assign {
print('-=')
}
.mul_assign {
print('*=')
}
.div_assign {
print('/=')
}
.mod_assign {
print('%=')
}
.swap {
print('><')
}
}
}

pub struct IRAssign {
pub mut:
id IID
Expand Down Expand Up @@ -252,6 +300,7 @@ pub struct IRJump {
pub mut:
id IID
target BBID
// TODO: add value here for calling the .mcfunction with args
}

pub struct IRBranch {
Expand All @@ -260,6 +309,7 @@ pub mut:
cond OID
then BBID
el BBID
// TODO: add values here for calling the .mcfunction with args
}

pub struct IRReturn {
Expand Down Expand Up @@ -304,6 +354,7 @@ pub mut:
text string
}

// TODO: make it so that all string macros can be any expression just inside of a macro
pub struct IRStringMacro {
pub mut:
value RID
Expand Down Expand Up @@ -355,14 +406,50 @@ pub struct IRRef {
pub mut:
id RID
value IRRefSum
typ IRType
}

// IR OPERAND
type OID = int
pub type IROperand = RID | IRConstant
type OID = RID | CID

@[inline]
pub fn (id OID) to_ir_type(f IRFunction) IRType {
match id {
RID {
return f.refs[id].typ
}
CID {
match f.consts[id] {
IRIntConst {
return BuiltinType.int_t
}
IRCharConst {
return BuiltinType.char_t
}
IRFloatConst {
return BuiltinType.float_t
}
IRStringConst {
return BuiltinType.string_t
}
IRListConst {
return BuiltinType.list_t
}
IRDictConst {
return BuiltinType.dict_t
}
else {
panic('Type not implemented in oid to ir type ${f.consts[id]}')
}
}
}
}
}

type CID = int
pub type IRConstant = IRIntConst
| IRFloatConst
| IRCharConst
| IRStringConst
| IRListConst
| IRDictConst
Expand All @@ -373,14 +460,18 @@ pub mut:
value int
}

pub struct IRCharConst {
pub mut:
value string
}

pub struct IRFloatConst {
pub mut:
value f64
}

pub struct IRStringConst {
pub mut:
value string
parts []IRStringPart // For interpolated strings
}

Expand Down Expand Up @@ -850,6 +941,9 @@ pub fn (mut b IRBuilder) literal_has_macro(l Literal) bool {
}
}
}
FloatLiteral {
return false
}
CharLiteral {
return false
}
Expand Down Expand Up @@ -900,6 +994,9 @@ pub fn (mut b IRBuilder) literal_needs_block(l Literal) bool {
IntegerLiteral {
return false
}
FloatLiteral {
return false
}
StringLiteral {
if !l.interpolated {
return false
Expand Down
Loading