Skip to content

Commit

Permalink
loading string literal with randomly generated label id
Browse files Browse the repository at this point in the history
  • Loading branch information
rigel-star committed Jul 25, 2024
1 parent b710dda commit 6a09905
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 102 deletions.
2 changes: 1 addition & 1 deletion examples/input1.bic
Original file line number Diff line number Diff line change
@@ -1 +1 @@
let a: integer = 88834;
let b = 34;
19 changes: 19 additions & 0 deletions src/code_gen/aarch64/aarch64_codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

use core::panic;
use std::cell::RefMut;
use std::cell::RefCell;
use std::rc::Rc;
Expand Down Expand Up @@ -69,6 +70,13 @@ impl<'aarch64> CodeGen for Aarch64CodeGen<'aarch64> {
if symbol.lit_type == LitTypeVariant::None || symbol.sym_type == SymbolType::Function || symbol.class != StorageClass::GLOBAL {
continue;
}
if symbol.lit_type == LitTypeVariant::Str && symbol.sym_type == SymbolType::Constant {
let str_const_name_and_val: Vec<&str> = symbol.name.split("---").collect::<Vec<&str>>();
let str_const_name: String = String::from(str_const_name_and_val[0]);
println!(".data\n.global {str_const_name}");
println!("{str_const_name}:\n\t.asciz \"{}\"", str_const_name_and_val[1]);
continue;
}
println!(".data\n.global {}", symbol.name);
if symbol.sym_type == SymbolType::Variable {
Aarch64CodeGen::dump_global_with_alignment(symbol);
Expand Down Expand Up @@ -427,6 +435,17 @@ impl<'aarch64> Aarch64CodeGen<'aarch64> {
match symbol.lit_type {
LitTypeVariant::I32 => println!("{}: .align 4\n\t.word 0", symbol.name),
LitTypeVariant::U8 => println!("{}:\t.byte 0", symbol.name),
LitTypeVariant::Str => {
let label_id = if let Some(lit_val) = &symbol.default_value {
match lit_val {
LitType::I32(__id) => *__id,
_ => panic!("Not a valid label id for string literal '{}'", symbol.default_value.as_ref().unwrap())
}
} else {
panic!("No label id provided for string literal");
};
println!("{}:\t.quad ._L{:?}", symbol.name, label_id);
}
_ => panic!("Symbol's size is not supported right now: '{:?}'", symbol),
}
}
Expand Down
39 changes: 21 additions & 18 deletions src/code_gen/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,26 @@ pub trait CodeGen {
self.reg_manager().deallocate_all();
}
return 0xFFFFFFFF;
} else if ast_node.operation == ASTOperation::AST_VAR_DECL {
}
else if ast_node.operation == ASTOperation::AST_RETURN {
let possible_ret_stmt: Stmt = ast_node.kind.clone().unwrap_stmt();
let return_expr: &AST = ast_node.left.as_ref().unwrap();
let result_reg: usize = self.gen_expr(&return_expr.kind.clone().unwrap_expr(), ast_node.operation, reg, parent_ast_kind);
return match possible_ret_stmt {
Stmt::Return(ret) => self.gen_return_stmt(result_reg, ret.func_id),
_ => 0xFFFFFFFF
};
}
else if ast_node.operation == ASTOperation::AST_NONE || ast_node.operation == ASTOperation::AST_VAR_DECL {
return 0xFFFFFFFF
}
else {
let expr_ast: Expr = ast_node.kind.clone().unwrap_expr();
let reg_used_for_expr: usize = self.gen_expr(&expr_ast, ast_node.operation, reg, parent_ast_kind);
return reg_used_for_expr;
}
/*
else if ast_node.operation == ASTOperation::AST_VAR_DECL {
let possible_var_decl_stmt: Stmt = ast_node.kind.clone().unwrap_stmt();
return match possible_var_decl_stmt {
Stmt::VarDecl(var_decl) => {
Expand All @@ -137,23 +156,7 @@ pub trait CodeGen {
_ => 0xFFFFFFFF // invalid AST kind with AST_LVIDENT operation
};
}
else if ast_node.operation == ASTOperation::AST_RETURN {
let possible_ret_stmt: Stmt = ast_node.kind.clone().unwrap_stmt();
let return_expr: &AST = ast_node.left.as_ref().unwrap();
let result_reg: usize = self.gen_expr(&return_expr.kind.clone().unwrap_expr(), ast_node.operation, reg, parent_ast_kind);
return match possible_ret_stmt {
Stmt::Return(ret) => self.gen_return_stmt(result_reg, ret.func_id),
_ => 0xFFFFFFFF
};
}
else if ast_node.operation == ASTOperation::AST_NONE {
return 0xFFFFFFFF
}
else {
let expr_ast: Expr = ast_node.kind.clone().unwrap_expr();
let reg_used_for_expr: usize = self.gen_expr(&expr_ast, ast_node.operation, reg, parent_ast_kind);
return reg_used_for_expr;
}
*/
}

fn gen_expr(
Expand Down
6 changes: 3 additions & 3 deletions src/context/compiler_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct CompilerCtx<'ctx> {
pub label_id: usize,

/// Source file that is currently being processed.
pub current_file: Option<&'ctx SourceFile>
pub current_file: Option<&'ctx SourceFile>,
}

impl<'ctx> CompilerCtx<'ctx> {
Expand All @@ -45,11 +45,11 @@ impl<'ctx> CompilerCtx<'ctx> {
sym_table: symt,
func_table,
label_id: 0,
current_file: None
current_file: None,
}
}

pub fn incr_label_count(&mut self) {
self.label_id += 1;
}
}
}
6 changes: 4 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ pub enum BTypeErr {
TypesMismatch { expected: String, found: String },
AssignmentTypeMismatch { var_type: String, assigned_type: String },
ReturnTypeMismatch { expected: String, found: String },
IncompatibleTypes { first_type: String, second_type: String, operator: String }
IncompatibleTypes { first_type: String, second_type: String, operator: String },
InitializerNotAConstant { lexeme: String }
}

#[derive(PartialEq, Clone, Debug)]
Expand Down Expand Up @@ -91,7 +92,8 @@ impl BErr {
BTypeErr::TypesMismatch { expected, found } => format!("Type mismatch: expected {}, found {}", expected, found),
BTypeErr::AssignmentTypeMismatch { var_type, assigned_type } => format!("Cannot assign a value of type '{}' to a variable of type '{}'", assigned_type, var_type),
BTypeErr::ReturnTypeMismatch { expected, found } => format!("Return type mismatch: expected {}, found {}", expected, found),
BTypeErr::IncompatibleTypes { first_type, second_type, operator } => format!("Incompatible types '{}' and '{}' for operator '{}'", first_type, second_type, operator)
BTypeErr::IncompatibleTypes { first_type, second_type, operator } => format!("Incompatible types '{}' and '{}' for operator '{}'", first_type, second_type, operator),
BTypeErr::InitializerNotAConstant { lexeme } => format!("Initializer is not a constant '{}'", lexeme)
},
_ => "".to_string()
};
Expand Down
Loading

0 comments on commit 6a09905

Please sign in to comment.