Skip to content

Commit

Permalink
initial str type
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Apr 13, 2024
1 parent c568cd4 commit bd19ba6
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 7 deletions.
97 changes: 92 additions & 5 deletions lib/edlang_codegen_llvm/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub fn compile(session: &Session, program: &ProgramBody) -> Result<PathBuf, Box<
if session.library {
inkwell::targets::RelocMode::DynamicNoPic
} else {
inkwell::targets::RelocMode::Default
inkwell::targets::RelocMode::PIC
},
inkwell::targets::CodeModel::Default,
)
Expand Down Expand Up @@ -1238,9 +1238,9 @@ fn compile_load_place<'ctx>(
fn compile_value<'ctx>(
ctx: &ModuleCompileCtx<'ctx, '_>,
val: &ValueTree,
ty: &ir::TypeInfo,
type_info: &ir::TypeInfo,
) -> Result<BasicValueEnum<'ctx>, BuilderError> {
let ty = compile_basic_type(ctx, ty);
let ty = compile_basic_type(ctx, type_info);
Ok(match val {
ValueTree::Leaf(const_val) => match const_val {
ir::ConstValue::Bool(x) => ty
Expand Down Expand Up @@ -1298,6 +1298,23 @@ fn compile_value<'ctx>(
.into_int_type()
.const_int((*x) as u64, true)
.as_basic_value_enum(),
ir::ConstValue::Str(x) => {
let inner_ty = match &type_info.kind {
ir::TypeKind::Ref(_, inner) => &**inner,
_ => unreachable!(),
};
let inner_ty = compile_basic_type(ctx, inner_ty);
let x = &x[1..x.len() - 1];
let ptr = ctx.builder.build_global_string_ptr(x, "")?;
let len = ctx.ctx.context.i64_type().const_int(x.len() as u64, false);
let value = inner_ty
.into_struct_type()
.const_named_struct(&[ptr.as_basic_value_enum(), len.as_basic_value_enum()])
.as_basic_value_enum();
let ptr = ctx.builder.build_alloca(inner_ty, "")?;
ctx.builder.build_store(ptr, value)?;
ptr.as_basic_value_enum()
}
},
ValueTree::Branch(_) => todo!(),
})
Expand Down Expand Up @@ -1393,7 +1410,35 @@ fn compile_basic_type<'ctx>(
.struct_type(&fields, false)
.as_basic_type_enum()
}
ir::TypeKind::Str => todo!(),
ir::TypeKind::Str => {
let fields = [
compile_basic_type(
ctx,
&ir::TypeInfo {
span: None,
kind: ir::TypeKind::Ptr(
true,
Box::new(ir::TypeInfo {
span: None,
kind: ir::TypeKind::Uint(ir::UintTy::U8),
}),
),
},
),
compile_basic_type(
ctx,
&ir::TypeInfo {
span: None,
kind: ir::TypeKind::Uint(ir::UintTy::U64),
},
),
];

ctx.ctx
.context
.struct_type(&fields, false)
.as_basic_type_enum()
}
}
}

Expand Down Expand Up @@ -1545,6 +1590,48 @@ fn compile_debug_type<'ctx>(ctx: &ModuleCompileCtx<'ctx, '_>, ty: &ir::TypeInfo)
)
.as_type()
}
ir::TypeKind::Str => todo!(),
ir::TypeKind::Str => {
let fields = [
compile_debug_type(
ctx,
&ir::TypeInfo {
span: None,
kind: ir::TypeKind::Ptr(
true,
Box::new(ir::TypeInfo {
span: None,
kind: ir::TypeKind::Uint(ir::UintTy::U8),
}),
),
},
),
compile_debug_type(
ctx,
&ir::TypeInfo {
span: None,
kind: ir::TypeKind::Uint(ir::UintTy::U64),
},
),
];

let real_ty = compile_basic_type(ctx, ty);

ctx.di_builder
.create_struct_type(
ctx.di_namespace,
"str",
ctx.di_unit.get_file(),
0,
ctx.target_data.get_bit_size(&real_ty),
ctx.target_data.get_abi_alignment(&real_ty),
0,
None,
&fields,
0,
None,
"str",
)
.as_type()
}
}
}
4 changes: 3 additions & 1 deletion lib/edlang_ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ impl ValueTree {
ConstValue::F64(_) => TypeKind::Float(FloatTy::F64),
ConstValue::Char(_) => TypeKind::Char,
ConstValue::Isize(_) => TypeKind::Int(IntTy::Isize),
ConstValue::Str(_) => TypeKind::Str,
},
ValueTree::Branch(_) => todo!(),
}
Expand Down Expand Up @@ -534,7 +535,7 @@ pub enum UnOp {
Neg,
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone)]
pub enum ConstValue {
Bool(bool),
Char(char),
Expand All @@ -551,4 +552,5 @@ pub enum ConstValue {
U128(u128),
F32(f32),
F64(f64),
Str(String),
}
27 changes: 26 additions & 1 deletion lib/edlang_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,32 @@ fn lower_value(
},
None => todo!(),
},
ast::ValueExpr::Str { value: _, span: _ } => todo!(),
ast::ValueExpr::Str { value, span } => {
let ty = match type_hint {
Some(ty) => ty.clone(),
None => ir::TypeInfo {
span: None,
kind: ir::TypeKind::Ref(
false,
Box::new(ir::TypeInfo {
span: None,
kind: ir::TypeKind::Str,
}),
),
},
};
(
ir::Operand::Constant(ir::ConstData {
span: Some(*span),
type_info: ty.clone(),
kind: ir::ConstKind::Value(ir::ValueTree::Leaf(ir::ConstValue::Str(
value.clone(),
))),
}),
ty.kind,
*span,
)
}
ast::ValueExpr::Path(info) => {
let (place, ty, span) = lower_path(builder, info)?;
(Operand::Move(place), ty, span)
Expand Down
Binary file removed program
Binary file not shown.

0 comments on commit bd19ba6

Please sign in to comment.