Skip to content

Commit

Permalink
feat: support ffi_export attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
myuon committed Sep 10, 2023
1 parent 15d8089 commit 05f7c64
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions quartz/ast.qz
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ struct Function {
}?,
no_allocation: bool,
is_test: bool,
ffi_export: string?,
}

module Function {
Expand Down
10 changes: 10 additions & 0 deletions quartz/generator.qz
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ module Generator {
}]](),
result_type: self.main_signature_result!,
body: start_body,
ffi_export: nil,
});

self.start();
Expand Down Expand Up @@ -638,8 +639,17 @@ module Generator {
self.main_signature_result = func.result_type?;
}

if func.ffi_export != nil {
self.start();
self.write("export");
self.write(derive::to_string(func.ffi_export!));
self.write(format("(func ${})", func.name));
self.end();
}

self.start();
self.write("func");

self.write("$".concat(func.name));

for i in 0..func.params.length {
Expand Down
1 change: 1 addition & 0 deletions quartz/ir.qz
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ struct IrFunc {
}],
result_type: IrType,
body: vec[IrTerm],
ffi_export: string?,
}

struct IrCall {
Expand Down
5 changes: 5 additions & 0 deletions quartz/ir_code_gen.qz
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ module IrCodeGenerator {
},
},
}),
..nil,
},
});

Expand Down Expand Up @@ -302,6 +303,7 @@ module IrCodeGenerator {
t_address: true,
},
body: body,
..nil,
},
});
}
Expand Down Expand Up @@ -629,6 +631,7 @@ module IrCodeGenerator {
t_nil: true,
},
body: body,
..nil,
},
};
}
Expand Down Expand Up @@ -745,6 +748,7 @@ module IrCodeGenerator {
result_type: IrType {
t_nil: true,
},
..nil,
},
});

Expand Down Expand Up @@ -807,6 +811,7 @@ module IrCodeGenerator {
params: params,
result_type: IrType::new(func.result_type),
body: body,
ffi_export: func.ffi_export,
};
}

Expand Down
1 change: 1 addition & 0 deletions quartz/parser.qz
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ module Parser {
variadic: params.variadic,
no_allocation: false,
is_test: false,
ffi_export: nil,
},
location: Location {
start: start_token.location.start,
Expand Down
15 changes: 15 additions & 0 deletions quartz/preprocessor.qz
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ module Preprocessor {
}

next.t_func!.is_test = true;
} else if attr.name.data == "ffi_export" {
let next = m.decls.at(index + 1).data;
if next.t_func == nil {
return _ or error::new(
"expected function, but got {}".format(derive::to_string(next)),
);
}
if attr.args.length == 0 {
return _ or error::new("expected ffi_export");
}
if attr.args.at(0).data.t_string == nil {
return _ or error::new("expected string");
}

next.t_func!.ffi_export = attr.args.at(0).data.t_string!.value?;
} else {
return _ or error::new("unknown attribute");
}
Expand Down

0 comments on commit 05f7c64

Please sign in to comment.