commit 68787839e62702d0aef68f5ab18dc13afc4cffe2
parent 6e914e3fed3d94b2bd16a577850d8d56126b489e
Author: m21c <ho*******@gmail.com>
Date: Tue, 23 Jun 2026 04:02:01 +0200
add intrinsics for debugging
Diffstat:
| M | compiler.c | | | 60 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++----- |
1 file changed, 55 insertions(+), 5 deletions(-)
diff --git a/compiler.c b/compiler.c
@@ -8506,10 +8506,63 @@ handlelineending(Source *source)
}
static void
+makeintrinsic(Source *source, const char *name, Type *type, bool isfunction)
+{
+ Decl *r;
+ const int key = getstringkey(&idents, name, strlen(name));
+
+ if (isfunction) {
+ r = makedecl(source, key, DFUNCTION);
+ r->type = maketype(&source->arenas, &source->tok.loc,
+ primitive(TFUNCTION), NULL);
+ r->type->u.rtarget = type;
+ } else {
+ r = makedecl(source, key, DVAR);
+ r->type = type;
+ }
+}
+
+static void
+initintrinsics(Source *source)
+{
+ Type *tint = primitive(TINT);
+ Type *tvoid = primitive(TVOID);
+ Type *voidptr = maketype(&source->arenas, &source->tok.loc,
+ primitive(TPTR), primitive(TVOID));
+
+ #define intfunc(name, rtype) \
+ makeintrinsic(source, name, rtype, true)
+
+ intfunc("puts", tint);
+ intfunc("printf", tint);
+ intfunc("vprintf", tint);
+ intfunc("fprintf", tint);
+ intfunc("vfprintf", tint);
+ intfunc("sprintf", tint);
+ intfunc("vsprintf", tint);
+ intfunc("fopen", voidptr);
+ intfunc("ftell", tint);
+ intfunc("fseek", tint);
+ intfunc("fread", tint);
+ intfunc("fwrite", tint);
+ intfunc("fflush", tint);
+ intfunc("fclose", tint);
+
+ intfunc("calloc", voidptr);
+ intfunc("malloc", voidptr);
+ intfunc("realloc", voidptr);
+
+ intfunc("free", tvoid);
+
+ makeintrinsic(source, "SEEK_SET", tint, false);
+ makeintrinsic(source, "SEEK_CUR", tint, false);
+ makeintrinsic(source, "SEEK_END", tint, false);
+}
+
+static void
processfile(Source *source)
{
Block *block;
- Decl *intrinsic;
CodeGen cg = {0};
@@ -8517,10 +8570,7 @@ processfile(Source *source)
block = makeblock(BTOPLEVEL, source->currenv);
appendconduct(block, CSCOPE, NULL);
- intrinsic = makedecl(source, getstringkey(&idents, "printf", 6), DFUNCTION);
- intrinsic->type = maketype(&source->arenas, &source->tok.loc,
- primitive(TFUNCTION), NULL);
- intrinsic->type->u.rtarget = primitive(TINT);
+ initintrinsics(source);
cginit(&cg, source->currenv, fopen("out.c", "wb"));
while (getkind(source) != 0) {