Skip to content

Commit

Permalink
[FIX] fixing #38
Browse files Browse the repository at this point in the history
  • Loading branch information
Cr0a3 committed Oct 27, 2024
1 parent 056c3fb commit db46118
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 deletions.
8 changes: 3 additions & 5 deletions src/CodeGen/reg_alloc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl RegAlloc {

let mut num = 0;

for ty in &func.args {
for (name, ty) in &func.args {
let location = {
if self.just_vars {
self.curr_index += 1;
Expand Down Expand Up @@ -99,14 +99,12 @@ impl RegAlloc {
}
}

let name = || func.arg(num).name;

self.vars.insert(
name(),
name.to_owned(),
location
);

self.var_types.insert(name(), *ty);
self.var_types.insert(name.to_owned(), *ty);

num += 1;
}
Expand Down
18 changes: 13 additions & 5 deletions src/IR/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::Support::ColorProfile;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FunctionType {
/// The function arguments (stored as: num, type)
pub args: Vec<TypeMetadata>,
pub args: Vec<(String, TypeMetadata)>,
/// The return type
pub ret: TypeMetadata,
/// After the given arguments any argument type can be supplied (like the printf function - is in c ...)
Expand All @@ -23,7 +23,7 @@ pub struct FunctionType {

impl FunctionType {
/// Creates a new function type
pub fn new(args: Vec<TypeMetadata>, ret: TypeMetadata) -> Self {
pub fn new(args: Vec<(String, TypeMetadata)>, ret: TypeMetadata) -> Self {
Self {
args: args,
ret: ret,
Expand All @@ -41,9 +41,9 @@ impl FunctionType {
/// If the num doesn't exists, it panics
pub fn arg(&self, num: usize) -> Var {
let mut index = 0;
for meta in &self.args {
for (name, meta) in &self.args {
if index == num {
return Var { name: format!("%{}", index), ty: *meta }
return Var { name: name.to_owned(), ty: *meta }
}

index += 1;
Expand Down Expand Up @@ -238,7 +238,15 @@ impl Function {

/// Creates a new function type
pub fn FnTy(args: Vec<TypeMetadata>, ret: TypeMetadata) -> FunctionType {
FunctionType::new(args, ret)
let mut processed = Vec::new();
let mut index = 0;

for arg in args {
processed.push((format!("%{index}"), arg));
index += 1;
}

FunctionType::new(processed, ret)
}

/// Creates a new Function
Expand Down
2 changes: 1 addition & 1 deletion src/IR/nodes/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Ir for Call<FuncId, Vec<Var>, Var> {
let args = &self.inner1.ty.args;
for arg in &self.inner2 {
if index < args.len() {
if matches!(args.get(index), Some(argty) if *argty != (*arg).ty.into()) {
if matches!(args.get(index), Some((_, argty)) if *argty != (*arg).ty) {
Err(VerifyError::InvalidArgumentTypeFound)?
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/IR/parser/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ impl IrGen {
fn gen_func(&mut self, name: String, ret: TypeMetadata, args: (BTreeMap<String, TypeMetadata>, bool), body: Vec<(String, IrBlock)>, scope: Linkage) {
let mut ty = FunctionType::new(vec![], ret);

for (_, arg) in &args.0 {
ty.args.push( *arg );
for (name, arg) in &args.0 {
ty.args.push( (name.to_owned(), *arg) );
}

let mut raw = Function {
Expand Down
14 changes: 9 additions & 5 deletions src/IR/parser/semnatic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::{BTreeMap, HashMap};

use crate::Obj::Linkage;
use crate::IR::{BlockId, Const, FnTy, FuncId, FunctionType, Type, TypeMetadata, Var};
use crate::IR::{BlockId, Const, FuncId, FunctionType, Type, TypeMetadata, Var};

use crate::prelude::ir::*;

Expand Down Expand Up @@ -58,11 +58,15 @@ impl<'a> IrSemnatic<'a> {

let mut fun_args = vec![];

for (_, arg) in &args.0 {
fun_args.push( *arg );
for (name, arg) in &args.0 {
fun_args.push( (name.to_owned(), *arg) );
}

let mut ty = FnTy(fun_args, ret);
let mut ty = FunctionType {
args: fun_args,
ret: ret,
any_args: false,
};

if args.1 {
ty.activate_dynamic_arguments();
Expand Down Expand Up @@ -392,7 +396,7 @@ impl<'a> IrSemnatic<'a> {
})?
};

if let Some(expected) = sig.args.get(index) {
if let Some((_, expected)) = sig.args.get(index) {
if *expected != *arg {
Err(IrError::WrongArgument {
loc: loc.to_owned(),
Expand Down
2 changes: 1 addition & 1 deletion src/Target/wasm/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl AsmPrinter for WasmAsmPrinter {

let mut index = 0;

for arg in &func.ty.args {
for (_name, arg) in &func.ty.args {
if index != 0 {
fmt_ty.push(',');
fmt_ty.push(' ');
Expand Down

0 comments on commit db46118

Please sign in to comment.