Skip to content

Commit

Permalink
Merge pull request #27 from Y-Nak/fix-gep
Browse files Browse the repository at this point in the history
Fix `gep` related calculation
  • Loading branch information
Y-Nak authored Sep 12, 2023
2 parents 117024e + d558e91 commit 8dddd21
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
17 changes: 12 additions & 5 deletions crates/ir/src/insn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ impl InsnData {

Self::Call { args, .. }
| Self::BrTable { args, .. }
| Self::Phi { values: args, .. } => args,
| Self::Phi { values: args, .. }
| Self::Gep { args } => args,

Self::Return { args } => args.as_ref().map(core::slice::from_ref).unwrap_or_default(),

Expand All @@ -308,7 +309,8 @@ impl InsnData {

Self::Call { args, .. }
| Self::BrTable { args, .. }
| Self::Phi { values: args, .. } => args,
| Self::Phi { values: args, .. }
| Self::Gep { args } => args,

Self::Return { args } => args.as_mut().map(core::slice::from_mut).unwrap_or_default(),

Expand Down Expand Up @@ -768,13 +770,18 @@ fn get_gep_result_type(dfg: &DataFlowGraph, base: Value, indices: &[Value]) -> T
});

let mut result_ty = base_ty;
for &index in indices {
for (i, &index) in indices.iter().enumerate() {
let Type::Compound(compound) = result_ty else {
unreachable!()
if indices.len() - i == 1 {
break;
} else {
unreachable!()
}
};

result_ty = ctx.with_ty_store(|s| match s.resolve_compound(compound) {
CompoundTypeData::Array { elem, .. } | CompoundTypeData::Ptr(elem) => *elem,
CompoundTypeData::Array { elem, .. } => *elem,
CompoundTypeData::Ptr(_) => result_ty,
CompoundTypeData::Struct(s) => {
let index = match dfg.value_data(index) {
ValueData::Immediate { imm, .. } => imm.as_usize(),
Expand Down
2 changes: 1 addition & 1 deletion crates/parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ impl<'a> Lexer<'a> {
fn try_eat_opcode(&mut self) -> Option<Code> {
try_eat_variant! {
self,
(b"gep", Code::Gep),
(b"not", Code::Not),
(b"neg", Code::Neg),
(b"add", Code::Add),
Expand Down Expand Up @@ -254,7 +255,6 @@ impl<'a> Lexer<'a> {
(b"fallthrough", Code::FallThrough),
(b"br_table", Code::BrTable),
(b"br", Code::Br),
(b"gep", Code::Gep),
(b"alloca", Code::Alloca),
(b"return", Code::Return),
(b"phi", Code::Phi),
Expand Down
11 changes: 11 additions & 0 deletions crates/parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,17 @@ mod tests {
));
}

#[test]
fn test_gep() {
assert!(test_func_parser(
"func public %test(v0.*i32, v1.*[*i64; 10]) -> *i32:
block0:
v2.*i32 = gep v0 10.i32;
v3.**i64 = gep v1 10.i32;
return v1;"
));
}

#[test]
fn parser_with_phi() {
assert!(test_func_parser(
Expand Down

0 comments on commit 8dddd21

Please sign in to comment.