Skip to content

Commit

Permalink
refine code
Browse files Browse the repository at this point in the history
  • Loading branch information
maekawatoshiki committed Jan 13, 2018
1 parent b3f3d69 commit c094a95
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
31 changes: 17 additions & 14 deletions src/codegen.rs
Expand Up @@ -16,7 +16,7 @@ use self::llvm::prelude::*;
use node;
use node::Bits;
use lexer::Pos;
use types::{Sign, StorageClass, Type};
use types::{RectypeName, Sign, StorageClass, Type};

macro_rules! matches {
($e:expr, $p:pat) => {
Expand Down Expand Up @@ -99,7 +99,7 @@ pub struct Codegen {
local_varmap: Vec<HashMap<String, VarInfo>>,
label_map: HashMap<String, LLVMBasicBlockRef>,
switch_list: VecDeque<(LLVMValueRef, LLVMBasicBlockRef, LLVMTypeRef)>,
llvm_struct_map: HashMap<String, RectypeInfo>,
llvm_struct_map: HashMap<RectypeName, RectypeInfo>,
break_labels: VecDeque<LLVMBasicBlockRef>,
continue_labels: VecDeque<LLVMBasicBlockRef>,
cur_func: Option<LLVMValueRef>,
Expand Down Expand Up @@ -237,7 +237,10 @@ impl Codegen {
node::ASTKind::VariableDecl(ref ty, ref name, ref sclass, ref init) => {
self.gen_global_var_decl(ty, name, sclass, init)
}
_ => panic!(format!("codegen: unknown ast (given {:?})", ast)),
_ => panic!(format!(
"given ast which shouldn't be contained in toplevels: {:?}",
ast
)),
};

result.or_else(|cr: Error| match cr {
Expand Down Expand Up @@ -447,10 +450,9 @@ impl Codegen {
} else {
// default initialization

match *ty {
// function is not initialized
Type::Func(_, _, _) => return Ok((gvar, Some(ty.clone()))),
_ => {}
if let Type::Func(_, _, _) = *ty {
// function must not be initialized
return Ok((gvar, Some(ty.clone())));
}

LLVMSetLinkage(
Expand Down Expand Up @@ -503,13 +505,14 @@ impl Codegen {
let elem = try!(self.gen(e)).0;
elems.push(self.typecast(elem, llvm_elem_ty));
}
let elems_len = elems.len();
Ok((
LLVMConstArray(
llvm_elem_ty,
elems.as_mut_slice().as_mut_ptr(),
elems.len() as u32,
elems_len as u32,
),
Some(Type::Array(Box::new(elem_ty.unwrap()), elems.len() as i32)),
Some(Type::Array(Box::new(elem_ty.unwrap()), elems_len as i32)),
))
}
unsafe fn gen_const_array_for_global_init(
Expand Down Expand Up @@ -599,7 +602,7 @@ impl Codegen {
try!(self.fill_with_0(var, ty));

for (i, e) in elems_ast.iter().enumerate() {
// TODO: makes very very no sense...
// TODO: makes no sense...
let load = LLVMBuildGEP(
self.builder,
var,
Expand Down Expand Up @@ -1638,11 +1641,11 @@ impl Codegen {
expr: &node::AST,
field_name: String,
) -> CodegenR<(LLVMValueRef, Option<Type>)> {
let (strct, ptr_ty_w) = try!(self.gen(expr));
let ptr_ty = ptr_ty_w.unwrap();
let (strct, ptr_ty) = try!(self.gen(expr));
let ptr_ty = ptr_ty.unwrap();
let ty = ptr_ty
.get_elem_ty()
.or_else(|| panic!("gen_assign: ptr_dst_ty must be a pointer to the value's type"))
.or_else(|| panic!("gen_assign: must be a pointer to the value's type"))
.unwrap();
let strct_name = ty.get_name();
assert!(strct_name.is_some());
Expand Down Expand Up @@ -1762,7 +1765,6 @@ impl Codegen {
for arg in &*args {
maybe_correct_args_val.push(try!(self.gen(arg)).0);
}
let args_len = args.len();

let func = match retrieve_from_load(ast) {
&node::AST {
Expand Down Expand Up @@ -1825,6 +1827,7 @@ impl Codegen {
LLVMGetParamTypes(llvm_functy, ptr_params_types);
let llvm_params_types = Vec::from_raw_parts(ptr_params_types, params_count, 0);

let args_len = args.len();
if !func_is_vararg && params_count < args_len {
return Err(Error::MsgWithPos(
"too many arguments".to_string(),
Expand Down
12 changes: 5 additions & 7 deletions src/parser.rs
Expand Up @@ -601,10 +601,10 @@ impl<'a> Parser<'a> {
}
}
fn read_string_initializer(&mut self, ty: &mut Type, string: String) -> ParseR<AST> {
let mut char_ary = Vec::new();
for c in string.chars() {
char_ary.push(AST::new(ASTKind::Char(c as i32), Pos::new(0, 0)));
}
let char_ary = string
.chars()
.map(|c| AST::new(ASTKind::Char(c as i32), Pos::new(0, 0)))
.collect::<Vec<AST>>();
if let &mut Type::Array(_, ref mut len) = ty {
*len = char_ary.len() as i32 + 1;
} else {
Expand Down Expand Up @@ -643,9 +643,7 @@ impl<'a> Parser<'a> {
self.lexer.get_cur_pos(),
))
} else {
// maybe, this block never reach though.
self.show_error("initializer of array must be array");
Err(Error::Something)
panic!()
}
}
fn read_struct_initializer(&mut self, ty: &mut Type) -> ParseR<AST> {
Expand Down
14 changes: 8 additions & 6 deletions src/types.rs
Expand Up @@ -27,13 +27,15 @@ pub enum Type {
Float,
Double,
Ptr(Box<Type>),
Array(Box<Type>, i32), // ary elem type, size
Func(Box<Type>, Vec<Type>, bool), // return type, param types, vararg
Struct(String, Vec<AST>), // name, fields
Union(String, Vec<AST>, usize), // name, fields, means size of nth field is size of the union
Enum, // as same as Int
Array(Box<Type>, i32), // ary elem type, size
Func(Box<Type>, Vec<Type>, bool), // return type, param types, vararg
Struct(RectypeName, Vec<AST>), // name, fields
Union(RectypeName, Vec<AST>, usize), // name, fields, means size of nth field is size of the union
Enum, // as same as Int
}

pub type RectypeName = String;

impl Type {
pub fn get_elem_ty<'a>(&'a self) -> Option<&'a Type> {
match self {
Expand Down Expand Up @@ -92,7 +94,7 @@ impl Type {
_ => None,
}
}
// TODO: I can't come up with a good name...
// TODO: any good name?
pub fn conversion(self) -> Type {
match self {
Type::Array(elem_ty, _) => Type::Ptr(elem_ty),
Expand Down

0 comments on commit c094a95

Please sign in to comment.