Skip to content

Commit

Permalink
Added the 'inline' attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
mauro-balades committed May 14, 2023
1 parent f3db87f commit 25d5b09
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 12 deletions.
Binary file modified bench/fib/fib
Binary file not shown.
2 changes: 1 addition & 1 deletion bench/fib/fib.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <cstdio>
#include <cstdint>

static uint64_t fib(uint64_t n) {
uint64_t fib(uint64_t n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
Expand Down
3 changes: 1 addition & 2 deletions bench/fib/fib.sn
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use Core::System

fn fib(n: i64) i64 {
static fn [[inline]] fib(n: i64) i64 {
if n <= 1 {
let a = 2
return n
}

Expand Down
6 changes: 4 additions & 2 deletions bench/fib/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ def total_time

languages << Language.new("sn", "snowball (compiled)", :compiled, "snowball build -f fib.sn -o O3", "./.sn/bin/out.o")

languages << Language.new("c", "C", :compiled, "gcc -O3 -o fib fib.c", "./fib")
languages << Language.new("cpp", "C++", :compiled, "clang++ -O3 -o fib fib.cpp", "./fib")
languages << Language.new("c (gnu)", "C", :compiled, "gcc -O3 -o fib fib.c", "./fib")
languages << Language.new("c (clang)", "C", :compiled, "clang-14 -O3 -o fib fib.c", "./fib")
languages << Language.new("cpp (gnu)", "C++", :compiled, "g++ -O3 -o fib fib.cpp", "./fib")
languages << Language.new("cpp (clang)", "C++", :compiled, "clang++-14 -O3 -o fib fib.cpp", "./fib")

languages << Language.new("py3", "Python3", :interpreted, "", "python3 fib.py")

Expand Down
1 change: 1 addition & 0 deletions src/ast/syntax/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Attributes {
enum Fn {
LLVM_FUNC,
INTERNAL_LINKAGE,
INLINE,
};
}

Expand Down
12 changes: 11 additions & 1 deletion src/builder/llvm/createLLVMFunction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ llvm::Function *LLVMBuilder::createLLVMFunction(ir::Func *func) {
? llvm::Function::InternalLinkage
: llvm::Function::ExternalLinkage,
name, module.get());
auto callee = (llvm::Function *)(fn);

auto callee = (llvm::Function *)fn;

auto attrSet = callee->getAttributes();

if (func->hasAttribute(Attributes::INLINE)) {
auto newAttrSet = attrSet.addFnAttribute(
callee->getContext(), llvm::Attribute::AlwaysInline);
callee->setAttributes(newAttrSet);
// TODO: other attributes
}

if (!ir::Func::isExternal(func->getMangle()) ||
func->getMangle() == "main") {
Expand Down
2 changes: 2 additions & 0 deletions src/parser/parseFunction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ FunctionDef *Parser::parseFunction(bool isConstructor, bool isOperator,
isLLVMFunction = true;
} else if (attr == "internal_linkage") {
attributes.push_back(Attributes::Fn::INTERNAL_LINKAGE);
} else if (attr == "inline") {
attributes.push_back(Attributes::Fn::INLINE);
} else {
createError<ATTRIBUTE_ERROR>(
"Trying to use an undefined attribute!",
Expand Down
10 changes: 4 additions & 6 deletions tests/main.sn
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use Core::System

fn fib(n: i64) i64 {
if n <= 1 {
return n
fn hello() {
asm {
mov arx, blah blah boring who tf cares about assembly
}

return fib(n-1) + fib(n-2)
}

pub fn main() i32 {
System::println(fib(47 as i64))
hello()
}

0 comments on commit 25d5b09

Please sign in to comment.