Compare commits

..

No commits in common. "02522dd36d34ab363d6d19824a40b60b776a4306" and "70a968d7a0995a20027366cd7e4ad143c3fced87" have entirely different histories.

4 changed files with 94 additions and 43 deletions

View File

@ -9,7 +9,6 @@ fn main() -> i32 {
i32::memcpy(potus, otus, 1); i32::memcpy(potus, otus, 1);
printf("log10 %f\n", f64::round(123.3) as f64); printf("log10 %f\n", f64::round(123.3) as f64);
printf("sqrt %f\n", f64::sqrt(2) as f64);
printf("log10 %f\n", potus[0] as f64); printf("log10 %f\n", potus[0] as f64);
return potus[0]; return potus[0];
} }

View File

@ -192,9 +192,13 @@ impl AnalysisState {
} }
} }
pub fn new_symbol(&mut self, definition: usize, kind: SemanticKind) -> SymbolId { pub fn new_symbol(&mut self, definition: usize, kind: SemanticKind, module_id: SourceModuleId) -> SymbolId {
let id = SymbolId(self.symbol_table.len()); let id = SymbolId(self.symbol_table.len());
self.symbol_table.push(Symbol { kind, definition }); self.symbol_table.push(Symbol {
kind,
definition,
module_id,
});
id id
} }
@ -219,6 +223,7 @@ impl AnalysisState {
pub struct Symbol { pub struct Symbol {
pub kind: SemanticKind, pub kind: SemanticKind,
pub definition: usize, pub definition: usize,
pub module_id: SourceModuleId,
} }
pub struct AnalysisScope<'a> { pub struct AnalysisScope<'a> {
@ -301,6 +306,7 @@ pub enum SemanticKind {
Type, Type,
Struct, Struct,
Comment, Comment,
Operator,
Keyword, Keyword,
Reference(SourceModuleId, SymbolId), Reference(SourceModuleId, SymbolId),
} }
@ -322,6 +328,7 @@ impl SemanticKind {
SemanticKind::Property => SemanticTokenType::PROPERTY, SemanticKind::Property => SemanticTokenType::PROPERTY,
SemanticKind::Struct => SemanticTokenType::STRUCT, SemanticKind::Struct => SemanticTokenType::STRUCT,
SemanticKind::Comment => SemanticTokenType::COMMENT, SemanticKind::Comment => SemanticTokenType::COMMENT,
SemanticKind::Operator => SemanticTokenType::OPERATOR,
SemanticKind::Keyword => SemanticTokenType::KEYWORD, SemanticKind::Keyword => SemanticTokenType::KEYWORD,
SemanticKind::Default => return None, SemanticKind::Default => return None,
SemanticKind::Reference(module_id, symbol_id) => { SemanticKind::Reference(module_id, symbol_id) => {
@ -353,6 +360,7 @@ impl SemanticKind {
SemanticKind::Property => SemanticTokenModifier::DECLARATION, SemanticKind::Property => SemanticTokenModifier::DECLARATION,
SemanticKind::Struct => SemanticTokenModifier::DEFINITION, SemanticKind::Struct => SemanticTokenModifier::DEFINITION,
SemanticKind::Comment => return None, SemanticKind::Comment => return None,
SemanticKind::Operator => return None,
SemanticKind::Keyword => return None, SemanticKind::Keyword => return None,
SemanticKind::Reference(..) => SEMANTIC_REFERENCE, SemanticKind::Reference(..) => SEMANTIC_REFERENCE,
}; };
@ -467,7 +475,7 @@ pub fn analyze_context(
_ => None, _ => None,
}; };
if let Some(semantic) = semantic_token { if let Some(semantic) = semantic_token {
let symbol = scope.state.new_symbol(i, semantic); let symbol = scope.state.new_symbol(i, semantic, module.module_id);
scope.state.set_symbol(i, symbol); scope.state.set_symbol(i, symbol);
} }
} }
@ -529,7 +537,9 @@ pub fn analyze_context(
let struct_idx = scope let struct_idx = scope
.token_idx(&typedef.meta, |t| matches!(t, Token::Identifier(_))) .token_idx(&typedef.meta, |t| matches!(t, Token::Identifier(_)))
.unwrap_or(typedef.meta.range.end); .unwrap_or(typedef.meta.range.end);
let struct_symbol = scope.state.new_symbol(struct_idx, SemanticKind::Struct); let struct_symbol = scope
.state
.new_symbol(struct_idx, SemanticKind::Struct, module.module_id);
scope.state.set_symbol(struct_idx, struct_symbol); scope.state.set_symbol(struct_idx, struct_symbol);
let ty = TypeKind::CustomType(CustomTypeKey(typedef.name.clone(), typedef.source_module)); let ty = TypeKind::CustomType(CustomTypeKey(typedef.name.clone(), typedef.source_module));
@ -553,7 +563,9 @@ pub fn analyze_context(
Some(field.1.clone()), Some(field.1.clone()),
); );
let field_symbol = scope.state.new_symbol(field_idx, SemanticKind::Property); let field_symbol = scope
.state
.new_symbol(field_idx, SemanticKind::Property, module.module_id);
scope.state.set_symbol(field_idx, field_symbol); scope.state.set_symbol(field_idx, field_symbol);
scope.state.properties.insert( scope.state.properties.insert(
@ -575,7 +587,7 @@ pub fn analyze_context(
let idx = scope let idx = scope
.token_idx(&param.meta, |t| matches!(t, Token::Identifier(_))) .token_idx(&param.meta, |t| matches!(t, Token::Identifier(_)))
.unwrap_or(param.meta.range.end); .unwrap_or(param.meta.range.end);
let symbol = scope.state.new_symbol(idx, SemanticKind::Variable); let symbol = scope.state.new_symbol(idx, SemanticKind::Variable, module.module_id);
scope.state.set_symbol(idx, symbol); scope.state.set_symbol(idx, symbol);
scope.variables.insert(param.name.clone(), symbol); scope.variables.insert(param.name.clone(), symbol);
} }
@ -605,7 +617,7 @@ pub fn analyze_context(
let idx = scope let idx = scope
.token_idx(&function.signature(), |t| matches!(t, Token::Identifier(_))) .token_idx(&function.signature(), |t| matches!(t, Token::Identifier(_)))
.unwrap_or(function.signature().range.end); .unwrap_or(function.signature().range.end);
let symbol = scope.state.new_symbol(idx, SemanticKind::Function); let symbol = scope.state.new_symbol(idx, SemanticKind::Function, module.module_id);
scope.state.set_symbol(idx, symbol); scope.state.set_symbol(idx, symbol);
scope scope
.state .state
@ -632,7 +644,9 @@ pub fn analyze_context(
let param_idx = inner_scope let param_idx = inner_scope
.token_idx(&param.meta, |t| matches!(t, Token::Identifier(_))) .token_idx(&param.meta, |t| matches!(t, Token::Identifier(_)))
.unwrap_or(function.signature().range.end); .unwrap_or(function.signature().range.end);
let param_symbol = inner_scope.state.new_symbol(param_idx, SemanticKind::Variable); let param_symbol = inner_scope
.state
.new_symbol(param_idx, SemanticKind::Variable, module.module_id);
inner_scope.state.set_symbol(param_idx, param_symbol); inner_scope.state.set_symbol(param_idx, param_symbol);
inner_scope.variables.insert(param.name.clone(), param_symbol); inner_scope.variables.insert(param.name.clone(), param_symbol);
} }
@ -664,7 +678,7 @@ pub fn analyze_context(
let idx = scope let idx = scope
.token_idx(&function.signature(), |t| matches!(t, Token::Identifier(_))) .token_idx(&function.signature(), |t| matches!(t, Token::Identifier(_)))
.unwrap_or(function.signature().range.end); .unwrap_or(function.signature().range.end);
let function_symbol = scope.state.new_symbol(idx, SemanticKind::Function); let function_symbol = scope.state.new_symbol(idx, SemanticKind::Function, module.module_id);
scope.state.set_symbol(idx, function_symbol); scope.state.set_symbol(idx, function_symbol);
scope.state.functions.insert(function.name.clone(), function_symbol); scope.state.functions.insert(function.name.clone(), function_symbol);
scope scope
@ -687,7 +701,9 @@ pub fn analyze_context(
let idx = inner_scope let idx = inner_scope
.token_idx(&param.meta, |t| matches!(t, Token::Identifier(_))) .token_idx(&param.meta, |t| matches!(t, Token::Identifier(_)))
.unwrap_or(function.signature().range.end); .unwrap_or(function.signature().range.end);
let symbol = inner_scope.state.new_symbol(idx, SemanticKind::Variable); let symbol = inner_scope
.state
.new_symbol(idx, SemanticKind::Variable, module.module_id);
inner_scope.state.set_symbol(idx, symbol); inner_scope.state.set_symbol(idx, symbol);
inner_scope.variables.insert(param.name.clone(), symbol); inner_scope.variables.insert(param.name.clone(), symbol);
} }
@ -728,7 +744,9 @@ pub fn analyze_block(
let idx = scope let idx = scope
.token_idx(&named_variable_ref.2, |t| matches!(t, Token::Identifier(_))) .token_idx(&named_variable_ref.2, |t| matches!(t, Token::Identifier(_)))
.unwrap_or(named_variable_ref.2.range.end); .unwrap_or(named_variable_ref.2.range.end);
let symbol = scope.state.new_symbol(idx, SemanticKind::Variable); let symbol = scope
.state
.new_symbol(idx, SemanticKind::Variable, source_module.module_id);
scope.state.set_symbol(idx, symbol); scope.state.set_symbol(idx, symbol);
scope.variables.insert(named_variable_ref.1.clone(), symbol); scope.variables.insert(named_variable_ref.1.clone(), symbol);
@ -776,11 +794,13 @@ pub fn analyze_expr(
.token_idx(&var_ref.2, |t| matches!(t, Token::Identifier(_))) .token_idx(&var_ref.2, |t| matches!(t, Token::Identifier(_)))
.unwrap_or(var_ref.2.range.end); .unwrap_or(var_ref.2.range.end);
let symbol = if let Some(symbol_id) = scope.variables.get(&var_ref.1) { let symbol = if let Some(symbol_id) = scope.variables.get(&var_ref.1) {
scope scope.state.new_symbol(
.state idx,
.new_symbol(idx, SemanticKind::Reference(source_module.module_id, *symbol_id)) SemanticKind::Reference(source_module.module_id, *symbol_id),
source_module.module_id,
)
} else { } else {
scope.state.new_symbol(idx, SemanticKind::Type) scope.state.new_symbol(idx, SemanticKind::Type, source_module.module_id)
}; };
scope.state.set_symbol(idx, symbol); scope.state.set_symbol(idx, symbol);
} }
@ -820,11 +840,15 @@ pub fn analyze_expr(
let field_symbol = if let Some((module_id, symbol_id)) = let field_symbol = if let Some((module_id, symbol_id)) =
scope.find_property(accessed_type.clone(), name.clone()) scope.find_property(accessed_type.clone(), name.clone())
{ {
scope.state.new_symbol(
field_idx,
SemanticKind::Reference(module_id, symbol_id),
source_module.module_id,
)
} else {
scope scope
.state .state
.new_symbol(field_idx, SemanticKind::Reference(module_id, symbol_id)) .new_symbol(field_idx, SemanticKind::Property, source_module.module_id)
} else {
scope.state.new_symbol(field_idx, SemanticKind::Property)
}; };
scope.state.set_symbol(field_idx, field_symbol); scope.state.set_symbol(field_idx, field_symbol);
@ -859,11 +883,15 @@ pub fn analyze_expr(
.unwrap_or(expr.1.range.end); .unwrap_or(expr.1.range.end);
let struct_symbol = if let Some(symbol_id) = scope.state.types.get(&struct_type) { let struct_symbol = if let Some(symbol_id) = scope.state.types.get(&struct_type) {
scope.state.new_symbol(
struct_idx,
SemanticKind::Reference(source_module.module_id, *symbol_id),
source_module.module_id,
)
} else {
scope scope
.state .state
.new_symbol(struct_idx, SemanticKind::Reference(source_module.module_id, *symbol_id)) .new_symbol(struct_idx, SemanticKind::Struct, source_module.module_id)
} else {
scope.state.new_symbol(struct_idx, SemanticKind::Struct)
}; };
scope.state.set_symbol(struct_idx, struct_symbol); scope.state.set_symbol(struct_idx, struct_symbol);
@ -874,11 +902,15 @@ pub fn analyze_expr(
let field_symbol = let field_symbol =
if let Some(symbol_id) = scope.state.properties.get(&(struct_type.clone(), field_name.clone())) { if let Some(symbol_id) = scope.state.properties.get(&(struct_type.clone(), field_name.clone())) {
scope.state.new_symbol(
field_idx,
SemanticKind::Reference(source_module.module_id, *symbol_id),
source_module.module_id,
)
} else {
scope scope
.state .state
.new_symbol(field_idx, SemanticKind::Reference(source_module.module_id, *symbol_id)) .new_symbol(field_idx, SemanticKind::Property, source_module.module_id)
} else {
scope.state.new_symbol(field_idx, SemanticKind::Property)
}; };
scope.state.set_symbol(field_idx, field_symbol); scope.state.set_symbol(field_idx, field_symbol);
@ -888,14 +920,18 @@ pub fn analyze_expr(
} }
mir::ExprKind::Literal(_) => { mir::ExprKind::Literal(_) => {
if let Some(idx) = scope.token_idx(&expr.1, |t| matches!(t, Token::StringLit(_) | Token::CharLit(_))) { if let Some(idx) = scope.token_idx(&expr.1, |t| matches!(t, Token::StringLit(_) | Token::CharLit(_))) {
scope.state.new_symbol(idx, SemanticKind::String); scope
.state
.new_symbol(idx, SemanticKind::String, source_module.module_id);
} else if let Some(idx) = scope.token_idx(&expr.1, |t| { } else if let Some(idx) = scope.token_idx(&expr.1, |t| {
matches!( matches!(
t, t,
Token::DecimalValue(_) | Token::HexadecimalValue(_) | Token::OctalValue(_) | Token::BinaryValue(_) Token::DecimalValue(_) | Token::HexadecimalValue(_) | Token::OctalValue(_) | Token::BinaryValue(_)
) )
}) { }) {
scope.state.new_symbol(idx, SemanticKind::Number); scope
.state
.new_symbol(idx, SemanticKind::Number, source_module.module_id);
} }
} }
mir::ExprKind::BinOp(_, lhs, rhs, _) => { mir::ExprKind::BinOp(_, lhs, rhs, _) => {
@ -913,11 +949,15 @@ pub fn analyze_expr(
.token_idx(&meta, |t| matches!(t, Token::Identifier(_))) .token_idx(&meta, |t| matches!(t, Token::Identifier(_)))
.unwrap_or(meta.range.end); .unwrap_or(meta.range.end);
let symbol = if let Some((module_id, symbol_id)) = scope.functions.get(name) { let symbol = if let Some((module_id, symbol_id)) = scope.functions.get(name) {
scope.state.new_symbol(
idx,
SemanticKind::Reference(*module_id, *symbol_id),
source_module.module_id,
)
} else {
scope scope
.state .state
.new_symbol(idx, SemanticKind::Reference(*module_id, *symbol_id)) .new_symbol(idx, SemanticKind::Function, source_module.module_id)
} else {
scope.state.new_symbol(idx, SemanticKind::Function)
}; };
scope.state.set_symbol(idx, symbol); scope.state.set_symbol(idx, symbol);
} }
@ -937,11 +977,15 @@ pub fn analyze_expr(
}; };
let type_symbol = if let Some((module_id, symbol_id)) = scope.types.get(&invoked_ty) { let type_symbol = if let Some((module_id, symbol_id)) = scope.types.get(&invoked_ty) {
scope.state.new_symbol(
type_idx,
SemanticKind::Reference(*module_id, *symbol_id),
source_module.module_id,
)
} else {
scope scope
.state .state
.new_symbol(type_idx, SemanticKind::Reference(*module_id, *symbol_id)) .new_symbol(type_idx, SemanticKind::Type, source_module.module_id)
} else {
scope.state.new_symbol(type_idx, SemanticKind::Type)
}; };
scope.state.set_symbol(type_idx, type_symbol); scope.state.set_symbol(type_idx, type_symbol);
@ -951,11 +995,15 @@ pub fn analyze_expr(
let fn_symbol = if let Some((module_id, symbol_id)) = let fn_symbol = if let Some((module_id, symbol_id)) =
scope.associated_functions.get(&(invoked_ty.clone(), name.clone())) scope.associated_functions.get(&(invoked_ty.clone(), name.clone()))
{ {
scope.state.new_symbol(
fn_idx,
SemanticKind::Reference(*module_id, *symbol_id),
source_module.module_id,
)
} else {
scope scope
.state .state
.new_symbol(fn_idx, SemanticKind::Reference(*module_id, *symbol_id)) .new_symbol(fn_idx, SemanticKind::Function, source_module.module_id)
} else {
scope.state.new_symbol(fn_idx, SemanticKind::Function)
}; };
scope.state.set_symbol(fn_idx, fn_symbol); scope.state.set_symbol(fn_idx, fn_symbol);

View File

@ -20,6 +20,7 @@ fn main() -> Result<(), std::io::Error> {
let mir_path = parent.with_extension("mir"); let mir_path = parent.with_extension("mir");
let asm_path = parent.with_extension("asm"); let asm_path = parent.with_extension("asm");
#[cfg(feature = "log_output")]
let before = std::time::SystemTime::now(); let before = std::time::SystemTime::now();
let text = fs::read_to_string(&path)?; let text = fs::read_to_string(&path)?;
@ -53,6 +54,8 @@ fn main() -> Result<(), std::io::Error> {
fs::write(&object_path, &obj_buffer).expect("Could not write Object-file!"); fs::write(&object_path, &obj_buffer).expect("Could not write Object-file!");
fs::write(&llir_path, &llir).expect("Could not write LLIR-file!"); fs::write(&llir_path, &llir).expect("Could not write LLIR-file!");
fs::write(&mir_path, &mir).expect("Could not write MIR-file!"); fs::write(&mir_path, &mir).expect("Could not write MIR-file!");
#[cfg(feature = "log_output")]
{
let after = std::time::SystemTime::now(); let after = std::time::SystemTime::now();
println!( println!(
"Compilation took: {:.2}ms\n", "Compilation took: {:.2}ms\n",
@ -60,6 +63,7 @@ fn main() -> Result<(), std::io::Error> {
); );
println!("Linking {:?}", &object_path); println!("Linking {:?}", &object_path);
}
let linker = std::env::var("LD").unwrap_or("ld".to_owned()); let linker = std::env::var("LD").unwrap_or("ld".to_owned());
let mut linker = LDRunner::from_command(&linker).with_library("c").with_library("m"); let mut linker = LDRunner::from_command(&linker).with_library("c").with_library("m");

View File

@ -721,7 +721,7 @@ impl Expression {
expr.resolve_ref(typerefs).cast_into(type_kind) expr.resolve_ref(typerefs).cast_into(type_kind)
} }
ExprKind::AssociatedFunctionCall(type_kind, function_call) => { ExprKind::AssociatedFunctionCall(type_kind, function_call) => {
*type_kind = type_kind.or_default()?; *type_kind = type_kind.or_default().unwrap();
let true_function = state let true_function = state
.scope .scope
.get_associated_function(&pass::AssociatedFunctionKey( .get_associated_function(&pass::AssociatedFunctionKey(