Allow to define a function that takes/returns flonums

This commit is contained in:
Rui Ueyama 2020-08-27 22:47:31 +09:00
parent 8ec1ebf176
commit c6b30568b4
2 changed files with 30 additions and 3 deletions

View File

@ -735,6 +735,18 @@ static void emit_data(Obj *prog) {
}
}
static void store_fp(int r, int offset, int sz) {
switch (sz) {
case 4:
println(" movss %%xmm%d, %d(%%rbp)", r, offset);
return;
case 8:
println(" movsd %%xmm%d, %d(%%rbp)", r, offset);
return;
}
unreachable();
}
static void store_gp(int r, int offset, int sz) {
switch (sz) {
case 1:
@ -803,9 +815,13 @@ static void emit_text(Obj *prog) {
}
// Save passed-by-register arguments to the stack
int i = 0;
for (Obj *var = fn->params; var; var = var->next)
store_gp(i++, var->offset, var->ty->size);
int gp = 0, fp = 0;
for (Obj *var = fn->params; var; var = var->next) {
if (is_flonum(var->ty))
store_fp(fp++, var->offset, var->ty->size);
else
store_gp(gp++, var->offset, var->ty->size);
}
// Emit code
gen_stmt(fn->body);

View File

@ -100,6 +100,14 @@ char *fmt(char *buf, char *fmt, ...) {
double add_double(double x, double y);
float add_float(float x, float y);
float add_float3(float x, float y, float z) {
return x + y + z;
}
double add_double3(double x, double y, double z) {
return x + y + z;
}
int main() {
ASSERT(3, ret3());
ASSERT(8, add2(3, 5));
@ -163,6 +171,9 @@ int main() {
ASSERT(6, add_float(2.3, 3.8));
ASSERT(6, add_double(2.3, 3.8));
ASSERT(7, add_float3(2.5, 2.5, 2.5));
ASSERT(7, add_double3(2.5, 2.5, 2.5));
printf("OK\n");
return 0;
}