Skip to content

Commit

Permalink
cilkcc: Refine code
Browse files Browse the repository at this point in the history
  • Loading branch information
maekawatoshiki committed Oct 9, 2020
1 parent a2cfa06 commit 32e78a4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
14 changes: 9 additions & 5 deletions cilkcc/examples/struct.c
@@ -1,9 +1,13 @@
int main() {
typedef struct {
int a;
int x, y, z;
} A;
A a;
a.a = 1;
a.a += a.a;
return a.a;
A a[1];
A c[2][2];
a[0].y=1;
A b;
b.x=3;
c[1][1].z=4;
c[0][1].y=1;
return b.x+a[0].y+c[1][1].z+c[0][1].y;
}
29 changes: 13 additions & 16 deletions cilkcc/src/codegen.rs
Expand Up @@ -2,7 +2,7 @@ use super::{
ast,
ast::AST,
token::SourceLoc,
types::{CompoundTypes, Sign, StorageClass, Type, TypeConversion},
types::{CompoundType, CompoundTypes, Sign, StorageClass, Type, TypeConversion},
};
use cilk::ir::{
builder::{IRBuilder, IRBuilderWithFunction, IRBuilderWithModuleAndFuncId},
Expand Down Expand Up @@ -300,7 +300,10 @@ impl<'a> FunctionCodeGenerator<'a> {
.compound_ty(id)
.as_pointer();
match inner {
types::Type::Array(_) => Ok((val, self.compound_types.pointer(ty))),
types::Type::Array(_) => Ok((val, {
let inner = self.compound_types[ty].inner_ty();
self.compound_types.pointer(inner)
})),
_ => Ok((
self.builder.build_load(val),
self.compound_types[ty].inner_ty(),
Expand Down Expand Up @@ -346,27 +349,21 @@ impl<'a> FunctionCodeGenerator<'a> {

fn generate_ptr_binary_op(
&mut self,
ty: Type,
mut ty: Type,
op: ast::BinaryOp,
lhs: Value,
rhs: Value,
) -> Result<(Value, Type)> {
let is_elem_array = match lhs.get_type() {
types::Type::Pointer(id) => matches!(
self.builder
.module()
.unwrap()
.types
.compound_ty(id)
.as_pointer(),
types::Type::Array(_)
),
_ => panic!(),
let inner = self.compound_types[ty].as_pointer();
let is_array = if let CompoundType::Array { inner, .. } = self.compound_types[inner] {
ty = self.compound_types.pointer(inner);
true
} else {
false
};
match op {
ast::BinaryOp::Add => {
if is_elem_array {
let ty = self.compound_types[ty].inner_ty();
if is_array {
Ok((
self.builder
.build_gep(lhs, vec![Value::new_imm_int32(0), rhs]),
Expand Down
4 changes: 4 additions & 0 deletions cilkcc/src/lib.rs
Expand Up @@ -60,6 +60,10 @@ pub fn compile(path: PathBuf) {
}
println!("{:?}", codegen.module);

// cilk::ir::mem2reg::Mem2Reg::new().run_on_module(&mut codegen.module);
// cilk::ir::cse::CommonSubexprElimination::new().run_on_module(&mut codegen.module);
// cilk::ir::licm::LoopInvariantCodeMotion::new().run_on_module(&mut codegen.module);

let machine_module =
cilk::codegen::x64::standard_conversion_into_machine_module(&mut codegen.module);
let mut printer = cilk::codegen::x64::asm::print::MachineAsmPrinter::new();
Expand Down

0 comments on commit 32e78a4

Please sign in to comment.