Skip to content

Commit

Permalink
Basic mathematical stdlib functionality (FuelLabs#61)
Browse files Browse the repository at this point in the history
* begin register allocator

* begin reg alloc

* mutable virtual registers; basic allocation algorithm skeleton

* mutable registers in allocation

* pull in fuel-asm official ops

* switching laptops

* begin work on virtual registers and ops

* daily checkpoint

* add AllocatedOp abstraction

* template for parsing ops

* allocation algorithm progress

* change op parsing logic

* WIP parsing inline asm to new ops

* more op parsing

* finish parsing virtual ops from asm

* start registers method

* register allocation method

* convert virtual registers to allocated ones

* switch back to organizational labels for jumps

* realized ops

* fully allocate registers and resolve labels

* print allocated registers

* fill in todo!() errors in asm parsing

* resolve all todosudo apt-get install vlc in core_lang

* switch to ssh for fuel-asm

* resolve warnings

* fix git url

* rustfmt

* small self-code-review

* resolve module

* map the virtual opcodes to fuel_asm ops

* code review feedback

* factor finalized asm out into its own file

* realize data section and instructions to bits

* data section offset label

* initial bytecode generation

* add forc --asm command

* print out the loading of the data section op

* resolve warnings

* fix register allocater bug

* cleanup

* fix bad error message

* code review feedback

* fix doctest

* fix typo

* reference fuel_core for register constants

* add stdlib stuff

* allow use of interface surface functions in the methods section of trait declarations

* comment

* basic math stdlib

* formatting

Co-authored-by: Alexander Hansen <[email protected]>
sezna and Alexander Hansen authored Jun 3, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 41c6abe commit d7be667
Showing 4 changed files with 286 additions and 87 deletions.
21 changes: 21 additions & 0 deletions core_lang/src/semantic_analysis/ast_node/declaration.rs
Original file line number Diff line number Diff line change
@@ -411,3 +411,24 @@ impl<'sc> TypedFunctionDeclaration<'sc> {
)
}
}

impl<'sc> TypedTraitFn<'sc> {
/// This function is used in trait declarations to insert "placeholder" functions
/// in the methods. This allows the methods to use functions declared in the
/// interface surface.
pub(crate) fn to_dummy_func(&self) -> TypedFunctionDeclaration<'sc> {
TypedFunctionDeclaration {
name: self.name.clone(),
body: TypedCodeBlock {
contents: vec![],
whole_block_span: self.name.span.clone(),
},
parameters: vec![],
span: self.name.span.clone(),
return_type: self.return_type.clone(),
return_type_span: self.return_type_span.clone(),
visibility: Visibility::Public,
type_parameters: vec![],
}
}
}
61 changes: 38 additions & 23 deletions core_lang/src/semantic_analysis/ast_node/mod.rs
Original file line number Diff line number Diff line change
@@ -171,28 +171,42 @@ impl<'sc> TypedAstNode<'sc> {
}) => {
let mut methods_buf = Vec::new();
let interface_surface = interface_surface
.into_iter()
.map(|TraitFn {
name,
parameters,
return_type,
return_type_span
}| TypedTraitFn {
name,
return_type_span,
parameters: parameters
.into_iter()
.map(|FunctionParameter { name, r#type, type_span }|
TypedFunctionParameter {
name,
r#type: namespace.resolve_type(&r#type,
&MaybeResolvedType::Partial(PartiallyResolvedType::SelfType)),
type_span }
).collect(),
return_type: namespace.resolve_type(&return_type,
&MaybeResolvedType::Partial(PartiallyResolvedType::SelfType)
)
}).collect::<Vec<_>>();
.into_iter()
.map(|TraitFn {
name,
parameters,
return_type,
return_type_span
}| TypedTraitFn {
name,
return_type_span,
parameters: parameters
.into_iter()
.map(|FunctionParameter { name, r#type, type_span }|
TypedFunctionParameter {
name,
r#type: namespace.resolve_type(&r#type,
&MaybeResolvedType::Partial(PartiallyResolvedType::SelfType)),
type_span }
).collect(),
return_type: namespace.resolve_type(&return_type,
&MaybeResolvedType::Partial(PartiallyResolvedType::SelfType)
)
}).collect::<Vec<_>>();
let mut l_namespace = namespace.clone();
// insert placeholder functions representing the interface surface
// to allow methods to use those functions
l_namespace.insert_trait_implementation(
CallPath {
prefixes: vec![],
suffix: name.clone(),
},
MaybeResolvedType::Partial(PartiallyResolvedType::SelfType),
interface_surface
.iter()
.map(|x| x.to_dummy_func())
.collect(),
);
for FunctionDeclaration {
body,
name: fn_name,
@@ -204,7 +218,7 @@ impl<'sc> TypedAstNode<'sc> {
..
} in methods
{
let mut namespace = namespace.clone();
let mut namespace = l_namespace.clone();
parameters.clone().into_iter().for_each(
|FunctionParameter { name, r#type, .. }| {
let r#type = namespace.resolve_type(
@@ -298,6 +312,7 @@ impl<'sc> TypedAstNode<'sc> {
},
)
.collect::<Vec<_>>();

// TODO check code block implicit return
let return_type = namespace.resolve_type(&return_type, self_type);
let (body, _code_block_implicit_return) = type_check!(
31 changes: 0 additions & 31 deletions forc/src/ops/forc_build.rs
Original file line number Diff line number Diff line change
@@ -14,37 +14,6 @@ use core_lang::{
};
use std::{fs, path::PathBuf};

pub fn print_asm(path: Option<String>) -> Result<(), String> {
// find manifest directory, even if in subdirectory
let this_dir = if let Some(path) = path {
PathBuf::from(path)
} else {
std::env::current_dir().unwrap()
};
let manifest_dir = find_manifest_dir(&this_dir).ok_or(format!(
"No manifest file found in this directory or any parent directories of it: {:?}",
this_dir
))?;
let manifest = read_manifest(&manifest_dir)?;

let mut namespace: Namespace = Default::default();
if let Some(ref deps) = manifest.dependencies {
for (dependency_name, dependency_details) in deps.iter() {
compile_dependency_lib(
&this_dir,
&dependency_name,
&dependency_details,
&mut namespace,
)?;
}
}

// now, compile this program with all of its dependencies
let main_file = get_main_file(&manifest, &manifest_dir)?;

Ok(())
}

pub fn build(command: BuildCommand) -> Result<Vec<u8>, String> {
let BuildCommand {
path,
Loading

0 comments on commit d7be667

Please sign in to comment.