Idris2/support/refc/runtime.c
Hattori, Hiroki 1dc7b74e4e
[RefC] Suppress arglist wrapper (#3177)
* [RefC] Suppress code generation for unnecessary arglist wrappers.

* [RefC] cleanup dead code of arglist.

* Removed Value_Arglist to reduce Closure's allocation overhead.

* fix linter error

* [RefC] make trampoline() safety.

* [RefC] cleanup cStatementsFromANF to keep code simple.

* fix linter error

* fix linter error

* In another time, another galaxy. THE LINTER INVADORS conquaer the all humanity and make them slaves. Under 2024, a only leaved job for every humans is adjusting spaces of source code, or just type gg0vG$== in vim.

* [ test ] update golden value

* added supports 32 params on closure.

* [RefC] [Cleanup] removing duplicate codes.

* [RefC] Switch calling conventions based on the number of arguments to avoid limits on the number of arguments and to reduce stack usage.

* [RefC] Argument that are too large are placed on the heap, as are closures.

* [RefC] use idris2_malloc instead of malloc.

* [RefC] [Cleanup] Keep pure things pure.

* [RefC] Mapped some special constructors to NULL. This reduces malloc cost and generates simpler code in ConCase. But not work yet.

* [RefC] fix merge failure.

* [RefC] stringOps.c replace  NULL for NIL.

* [RefC] cleanup

* [RefC] ConstCase now generate simple if-then statements instead of using helpers. This reduces malloc/free costs.

* fix indentation

* fix whitespaces

* [RefC] The name field in Value_Constructor was restored for tycon. But changed to static const*. Hopefully the C compiler will remove the common string constants. The smartest thing to do would be to create a dummy global variable and use its address as a tag, but that would depend on the C compiler to resolve conflicts.

* [refc] a big changes of the space

* [RefC] Little tricks to reduce temporary variables

* spaces

* [RefC] fix compiler warnings

* [RefC] [test]  Perform memory leak analysis, if valgrind is installed.

* [RefC] Fix invalid memory read. Fix C compiler warnings.

* [RefC] Fix invalid memory read of strSubstr. [test] Perform memory leak analysis, if valgrind is installed.

* [test] fix junk line

* linter

* linter

* linter

* linter

* [RefC] merge with erase_trivial_constuctors

* merge w/ erase_trivial_constructors

* Revert "merge w/ erase_trivial_constructors"

This reverts commit be593a3715.

* Revert "[RefC] merge with erase_trivial_constuctors"

This reverts commit 3c21eb45d8.

* merge w/ upstream/main

* fix merge failure

* rename

* fix renaming

* [RefC] fix merge fail

* [RefC] renamed C functions for safty.

* [RefC] cleanup

* [RefC] Fix constructor tag of UnconsResult.CHARACTER.

---------

Co-authored-by: Mathew Polzin <matt.polzin@gmail.com>
Co-authored-by: Guillaume Allais <guillaume.allais@ens-lyon.org>
2024-04-17 15:48:43 +01:00

140 lines
4.1 KiB
C

#include "runtime.h"
#include "_datatypes.h"
#include "refc_util.h"
void idris2_missing_ffi() {
fprintf(stderr, "Foreign function declared, but not defined.\n"
"Cannot call missing FFI - aborting.\n");
exit(1);
}
static inline Value *idris2_dispatch_closure(Value_Closure *clo) {
Value **const xs = clo->args;
Value *(*const f)() = clo->f;
switch (clo->arity) {
default:
return (*f)(xs);
case 0:
return (*f)();
case 1:
return (*f)(xs[0]);
case 2:
return (*f)(xs[0], xs[1]);
case 3:
return (*f)(xs[0], xs[1], xs[2]);
case 4:
return (*f)(xs[0], xs[1], xs[2], xs[3]);
case 5:
return (*f)(xs[0], xs[1], xs[2], xs[3], xs[4]);
case 6:
return (*f)(xs[0], xs[1], xs[2], xs[3], xs[4], xs[5]);
case 7:
return (*f)(xs[0], xs[1], xs[2], xs[3], xs[4], xs[5], xs[6]);
case 8:
return (*f)(xs[0], xs[1], xs[2], xs[3], xs[4], xs[5], xs[6], xs[7]);
case 9:
return (*f)(xs[0], xs[1], xs[2], xs[3], xs[4], xs[5], xs[6], xs[7], xs[8]);
case 10:
return (*f)(xs[0], xs[1], xs[2], xs[3], xs[4], xs[5], xs[6], xs[7], xs[8],
xs[9]);
case 11:
return (*f)(xs[0], xs[1], xs[2], xs[3], xs[4], xs[5], xs[6], xs[7], xs[8],
xs[9], xs[10]);
case 12:
return (*f)(xs[0], xs[1], xs[2], xs[3], xs[4], xs[5], xs[6], xs[7], xs[8],
xs[9], xs[10], xs[11]);
case 13:
return (*f)(xs[0], xs[1], xs[2], xs[3], xs[4], xs[5], xs[6], xs[7], xs[8],
xs[9], xs[10], xs[11], xs[12]);
case 14:
return (*f)(xs[0], xs[1], xs[2], xs[3], xs[4], xs[5], xs[6], xs[7], xs[8],
xs[9], xs[10], xs[11], xs[12], xs[13]);
case 15:
return (*f)(xs[0], xs[1], xs[2], xs[3], xs[4], xs[5], xs[6], xs[7], xs[8],
xs[9], xs[10], xs[11], xs[12], xs[13], xs[14]);
case 16:
return (*f)(xs[0], xs[1], xs[2], xs[3], xs[4], xs[5], xs[6], xs[7], xs[8],
xs[9], xs[10], xs[11], xs[12], xs[13], xs[14], xs[15]);
}
}
Value *idris2_trampoline(Value *it) {
while (it && !idris2_vp_is_unboxed(it) && it->header.tag == CLOSURE_TAG) {
Value_Closure *clos = (Value_Closure *)it;
if (clos->filled < clos->arity)
break;
it = idris2_dispatch_closure(clos);
if (idris2_isUnique(clos))
free(clos);
else
--clos->header.refCounter;
}
return it;
}
Value *idris2_tailcall_apply_closure(Value *_clos, Value *arg) {
// create a new closure and copy args.
Value_Closure *clos = (Value_Closure *)_clos;
Value_Closure *newclos = idris2_mkClosure(
clos->f, clos->arity, clos->filled + 1 /* expanding a payload */);
if (clos->header.refCounter <= 1) {
memcpy(newclos->args, clos->args, sizeof(Value *) * clos->filled);
} else {
// if the closure has multiple references, then apply newReference to
// arguments to avoid premature clearing of arguments
for (int i = 0; i < clos->filled; ++i)
newclos->args[i] = idris2_newReference(clos->args[i]);
}
newclos->args[clos->filled] = arg; // add argument to new arglist
if (idris2_isUnique(clos)) {
free(clos);
} else {
--clos->header.refCounter;
}
return (Value *)newclos;
}
Value *idris2_apply_closure(Value *_clos, Value *arg) {
return idris2_trampoline(idris2_tailcall_apply_closure(_clos, arg));
}
void idris2_removeReuseConstructor(Value_Constructor *constr) {
if (!constr) {
return;
}
IDRIS2_REFC_VERIFY(constr->header.refCounter > 0, "refCounter %lld",
(long long)constr->header.refCounter);
constr->header.refCounter--;
if (constr->header.refCounter == 0) {
free(constr);
}
}
int idris2_extractInt(Value *v) {
if (idris2_vp_is_unboxed(v))
return (int)idris2_vp_to_Int32(v);
switch (v->header.tag) {
case BITS32_TAG:
return (int)idris2_vp_to_Bits32(v);
case BITS64_TAG:
return (int)idris2_vp_to_Bits64(v);
case INT32_TAG:
return (int)idris2_vp_to_Bits32(v);
case INT64_TAG:
return (int)idris2_vp_to_Int64(v);
case INTEGER_TAG:
return (int)mpz_get_si(((Value_Integer *)v)->i);
case DOUBLE_TAG:
return (int)idris2_vp_to_Double(v);
default:
return -1;
}
}