1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-26 21:20:30 +02:00

Remove example .c files from documentation

This commit is contained in:
Andy Wingo 2018-10-30 11:33:58 +01:00
parent d90a545a58
commit 423d048b25
6 changed files with 0 additions and 344 deletions

View file

@ -1,75 +0,0 @@
#include <stdio.h>
#include <lightning.h>
static jit_state_t *_jit;
typedef long (*pwfw_t)(long); /* Pointer to Long Function of Long */
int main(int argc, char *argv[])
{
pwfw_t factorial;
long arg;
jit_node_t *ac; /* Accumulator */
jit_node_t *in; /* Argument */
jit_node_t *call;
jit_node_t *fact;
jit_node_t *jump;
jit_node_t *fact_entry;
jit_node_t *fact_out;
init_jit(argv[0]);
_jit = jit_new_state();
/* declare a forward label */
fact = jit_forward();
jit_prolog(); /* Entry point of the factorial function */
in = jit_arg(); /* Receive an integer argument */
jit_getarg(JIT_R0, in); /* Move argument to RO */
jit_prepare();
jit_pushargi(1); /* This is the accumulator */
jit_pushargr(JIT_R0); /* This is the argument */
call = jit_finishi(NULL); /* Call the tail call optimized function */
jit_patch_at(call, fact); /* Patch call to forward defined function */
/* the above could have been written as:
* jit_patch_at(jit_finishi(NULL), fact);
*/
jit_retval(JIT_R0); /* Fetch the result */
jit_retr(JIT_R0); /* Return it */
jit_epilog(); /* Epilog *before* label before prolog */
/* define the forward label */
jit_link(fact); /* Entry point of the helper function */
jit_prolog();
jit_frame(16); /* Reserve 16 bytes in the stack */
fact_entry = jit_label(); /* This is the tail call entry point */
ac = jit_arg(); /* The accumulator is the first argument */
in = jit_arg(); /* The factorial argument */
jit_getarg(JIT_R0, ac); /* Move the accumulator to R0 */
jit_getarg(JIT_R1, in); /* Move the argument to R1 */
fact_out = jit_blei(JIT_R1, 1); /* Done if argument is one or less */
jit_mulr(JIT_R0, JIT_R0, JIT_R1); /* accumulator *= argument */
jit_putargr(JIT_R0, ac); /* Update the accumulator */
jit_subi(JIT_R1, JIT_R1, 1); /* argument -= 1 */
jit_putargr(JIT_R1, in); /* Update the argument */
jump = jit_jmpi();
jit_patch_at(jump, fact_entry); /* Tail Call Optimize it! */
jit_patch(fact_out);
jit_retr(JIT_R0); /* Return the accumulator */
factorial = jit_emit();
/* no need to query information about resolved addresses */
jit_clear_state();
if (argc == 2)
arg = atoi(argv[1]);
else
arg = 5;
/* call the generated code */
printf("factorial(%ld) = %ld\n", arg, factorial(arg));
/* release all memory associated with the _jit identifier */
jit_destroy_state();
finish_jit();
return 0;
}

View file

@ -1,49 +0,0 @@
#include <stdio.h>
#include <lightning.h>
static jit_state_t *_jit;
typedef int (*pifi)(int); /* Pointer to Int Function of Int */
int main(int argc, char *argv[])
{
pifi fib;
jit_node_t *in; /* offset of the argument */
jit_node_t *ref; /* to patch the forward reference */
jit_node_t *zero; /* to patch the forward reference */
jit_node_t *jump; /* jump to start of loop */
jit_node_t *loop; /* start of the loop */
init_jit(argv[0]);
_jit = jit_new_state();
jit_prolog ();
in = jit_arg ();
jit_getarg (JIT_R0, in); /* R0 = n */
zero = jit_beqi (JIT_R0, 0);
jit_movr (JIT_R1, JIT_R0);
jit_movi (JIT_R0, 1);
ref = jit_blei (JIT_R1, 2);
jit_subi (JIT_R2, JIT_R1, 2);
jit_movr (JIT_R1, JIT_R0);
loop= jit_label();
jit_subi (JIT_R2, JIT_R2, 1); /* decr. counter */
jit_movr (JIT_V0, JIT_R0); /* V0 = R0 */
jit_addr (JIT_R0, JIT_R0, JIT_R1); /* R0 = R0 + R1 */
jit_movr (JIT_R1, JIT_V0); /* R1 = V0 */
jump= jit_bnei (JIT_R2, 0); /* if (R2) goto loop; */
jit_patch_at(jump, loop);
jit_patch(ref); /* patch forward jump */
jit_patch(zero); /* patch forward jump */
jit_retr (JIT_R0);
/* call the generated code, passing 36 as an argument */
fib = jit_emit();
jit_clear_state();
printf("fib(%d) = %d\n", 36, fib(36));
jit_destroy_state();
finish_jit();
return 0;
}

View file

@ -1,31 +0,0 @@
#include <stdio.h>
#include <lightning.h>
static jit_state_t *_jit;
typedef int (*pifi)(int); /* Pointer to Int Function of Int */
int main(int argc, char *argv[])
{
jit_node_t *in;
pifi incr;
init_jit(argv[0]);
_jit = jit_new_state();
jit_prolog(); /* @t{ prolog } */
in = jit_arg(); /* @t{ in = arg } */
jit_getarg(JIT_R0, in); /* @t{ getarg R0 } */
jit_addi(JIT_R0, JIT_R0, 1); /* @t{ addi R0\, R0\, 1 } */
jit_retr(JIT_R0); /* @t{ retr R0 } */
incr = jit_emit();
jit_clear_state();
/* call the generated code\, passing 5 as an argument */
printf("%d + 1 = %d\n", 5, incr(5));
jit_destroy_state();
finish_jit();
return 0;
}

View file

@ -1,40 +0,0 @@
#include <stdio.h>
#include <lightning.h>
static jit_state_t *_jit;
typedef void (*pvfi)(int); /* Pointer to Void Function of Int */
int main(int argc, char *argv[])
{
pvfi myFunction; /* ptr to generated code */
jit_node_t *start, *end; /* a couple of labels */
jit_node_t *in; /* to get the argument */
init_jit(argv[0]);
_jit = jit_new_state();
start = jit_note(__FILE__, __LINE__);
jit_prolog();
in = jit_arg();
jit_getarg(JIT_R1, in);
jit_pushargi((jit_word_t)"generated %d bytes\n");
jit_ellipsis();
jit_pushargr(JIT_R1);
jit_finishi(printf);
jit_ret();
jit_epilog();
end = jit_note(__FILE__, __LINE__);
myFunction = jit_emit();
/* call the generated code, passing its size as argument */
myFunction((char*)jit_address(end) - (char*)jit_address(start));
jit_clear_state();
jit_disassemble();
jit_destroy_state();
finish_jit();
return 0;
}

View file

@ -1,53 +0,0 @@
#include <stdio.h>
#include <lightning.h>
static jit_state_t *_jit;
typedef int (*pifi)(int); /* Pointer to Int Function of Int */
int main(int argc, char *argv[])
{
pifi fib;
jit_node_t *label;
jit_node_t *call;
jit_node_t *in; /* offset of the argument */
jit_node_t *ref; /* to patch the forward reference */
jit_node_t *zero; /* to patch the forward reference */
init_jit(argv[0]);
_jit = jit_new_state();
label = jit_label();
jit_prolog ();
in = jit_arg ();
jit_getarg (JIT_R0, in); /* R0 = n */
zero = jit_beqi (JIT_R0, 0);
jit_movr (JIT_V0, JIT_R0); /* V0 = R0 */
jit_movi (JIT_R0, 1);
ref = jit_blei (JIT_V0, 2);
jit_subi (JIT_V1, JIT_V0, 1); /* V1 = n-1 */
jit_subi (JIT_V2, JIT_V0, 2); /* V2 = n-2 */
jit_prepare();
jit_pushargr(JIT_V1);
call = jit_finishi(NULL);
jit_patch_at(call, label);
jit_retval(JIT_V1); /* V1 = fib(n-1) */
jit_prepare();
jit_pushargr(JIT_V2);
call = jit_finishi(NULL);
jit_patch_at(call, label);
jit_retval(JIT_R0); /* R0 = fib(n-2) */
jit_addr(JIT_R0, JIT_R0, JIT_V1); /* R0 = R0 + V1 */
jit_patch(ref); /* patch jump */
jit_patch(zero); /* patch jump */
jit_retr(JIT_R0);
/* call the generated code, passing 32 as an argument */
fib = jit_emit();
jit_clear_state();
printf("fib(%d) = %d\n", 32, fib(32));
jit_destroy_state();
finish_jit();
return 0;
}

View file

@ -1,96 +0,0 @@
#include <stdio.h>
#include <lightning.h>
typedef int (*pifi)(int); /* Pointer to Int Function of Int */
static jit_state_t *_jit;
void stack_push(int reg, int *sp)
{
jit_stxi_i (*sp, JIT_FP, reg);
*sp += sizeof (int);
}
void stack_pop(int reg, int *sp)
{
*sp -= sizeof (int);
jit_ldxi_i (reg, JIT_FP, *sp);
}
jit_node_t *compile_rpn(char *expr)
{
jit_node_t *in, *fn;
int stack_base, stack_ptr;
fn = jit_note(NULL, 0);
jit_prolog();
in = jit_arg();
stack_ptr = stack_base = jit_allocai (32 * sizeof (int));
jit_getarg_i(JIT_R2, in);
while (*expr) {
char buf[32];
int n;
if (sscanf(expr, "%[0-9]%n", buf, &n)) {
expr += n - 1;
stack_push(JIT_R0, &stack_ptr);
jit_movi(JIT_R0, atoi(buf));
} else if (*expr == 'x') {
stack_push(JIT_R0, &stack_ptr);
jit_movr(JIT_R0, JIT_R2);
} else if (*expr == '+') {
stack_pop(JIT_R1, &stack_ptr);
jit_addr(JIT_R0, JIT_R1, JIT_R0);
} else if (*expr == '-') {
stack_pop(JIT_R1, &stack_ptr);
jit_subr(JIT_R0, JIT_R1, JIT_R0);
} else if (*expr == '*') {
stack_pop(JIT_R1, &stack_ptr);
jit_mulr(JIT_R0, JIT_R1, JIT_R0);
} else if (*expr == '/') {
stack_pop(JIT_R1, &stack_ptr);
jit_divr(JIT_R0, JIT_R1, JIT_R0);
} else {
fprintf(stderr, "cannot compile: %s\n", expr);
abort();
}
++expr;
}
jit_retr(JIT_R0);
jit_epilog();
return fn;
}
int main(int argc, char *argv[])
{
jit_node_t *nc, *nf;
pifi c2f, f2c;
int i;
init_jit(argv[0]);
_jit = jit_new_state();
nc = compile_rpn("32x9*5/+");
nf = compile_rpn("x32-5*9/");
(void)jit_emit();
c2f = (pifi)jit_address(nc);
f2c = (pifi)jit_address(nf);
jit_clear_state();
printf("\nC:");
for (i = 0; i <= 100; i += 10) printf("%3d ", i);
printf("\nF:");
for (i = 0; i <= 100; i += 10) printf("%3d ", c2f(i));
printf("\n");
printf("\nF:");
for (i = 32; i <= 212; i += 18) printf("%3d ", i);
printf("\nC:");
for (i = 32; i <= 212; i += 18) printf("%3d ", f2c(i));
printf("\n");
jit_destroy_state();
finish_jit();
return 0;
}