tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] Local procedures


From: Zdenek Pavlas
Subject: Re: [Tinycc-devel] Local procedures
Date: Wed, 12 Sep 2007 12:54:16 +0200
User-agent: Thunderbird 1.5.0.12 (X11/20070530)

Antti-Juhani Kaijanaho wrote:
You only need the trampoline if you cannot make the function pointer fat
(ie. if you need to be binary compatible with vanilla C compilers).
Which, of course, describes tcc's situation fairly well.
Even if you really need a trampoline, you don't need a fancy compiler for that. It's easy to implement (at least on i386) using the existing support for attribute(regparm). Real closures in C- way more fun than gcc's nested functions, which are quite limited and (rightfully, IMO) seldom used.

#include <stdio.h>
#include <malloc.h>
#include <sys/mman.h>

void* closure(void *code, void *data) {
   char *c = malloc(10);
   c[0] = 0xb8, *(int*)(c + 1) = (int)data; // mov eax, ...
   c[5] = 0xe9, *(int*)(c + 6) = (char*)code - c - 10; // jmp ...
   mprotect((void*)((int)c & ~0xfff), 1, PROT_READ|PROT_WRITE|PROT_EXEC);
   return c;
}

void __attribute__((regparm(1))) /* expect arg #1 in eax */
add_to(int *data, int i) {
   *data += i;
}

int main() {
   int sum = 0, i;
   void (*fun)(int) = closure(add_to, &sum);
   for (i = 0; i < 10; i++) fun(i);
   free(fun);
   printf("%d\n", sum);
}

--
Zdenek Pavlas






reply via email to

[Prev in Thread] Current Thread [Next in Thread]