Skip to content

Commit

Permalink
Fix examples (#269)
Browse files Browse the repository at this point in the history
* Update examples to account for API changes

* Build examples cleanly without warnings

* Avoid ambiguous types in examples

Prefer sljit_* types over fundamental types, e.g., sljit_sw instead of
long. This is especially important on Win64 where sizeof(long) == 4.
  • Loading branch information
invertego authored Sep 1, 2024
1 parent 2c105e2 commit 7a6ceb3
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 111 deletions.
17 changes: 9 additions & 8 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ endif
CPPFLAGS = $(EXTRA_CPPFLAGS) -Isljit_src
CFLAGS += $(COMPAT_FLAGS) $(OPT_FLAGS) $(WARN_FLAGS) $(WERROR)
REGEX_CFLAGS += $(CFLAGS) -fshort-wchar
EXAMPLE_CFLAGS += $(CFLAGS) -Wno-unused-but-set-variable
LDFLAGS = $(EXTRA_LDFLAGS)

BINDIR = bin
Expand Down Expand Up @@ -85,25 +86,25 @@ $(BINDIR)/regex_test: $(BINDIR)/.keep $(BINDIR)/regexMain.o $(BINDIR)/regexJIT.o
examples: $(EXAMPLE_TARGET)

$(BINDIR)/first_program: $(EXAMPLEDIR)/first_program.c $(BINDIR)/.keep $(BINDIR)/sljitLir.o
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(EXAMPLEDIR)/first_program.c $(BINDIR)/sljitLir.o -o $@ -lm -lpthread $(EXTRA_LIBS)
$(CC) $(CPPFLAGS) $(EXAMPLE_CFLAGS) $(LDFLAGS) $(EXAMPLEDIR)/first_program.c $(BINDIR)/sljitLir.o -o $@ -lm -lpthread $(EXTRA_LIBS)

$(BINDIR)/branch: $(EXAMPLEDIR)/branch.c $(BINDIR)/.keep $(BINDIR)/sljitLir.o
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(EXAMPLEDIR)/branch.c $(BINDIR)/sljitLir.o -o $@ -lm -lpthread $(EXTRA_LIBS)
$(CC) $(CPPFLAGS) $(EXAMPLE_CFLAGS) $(LDFLAGS) $(EXAMPLEDIR)/branch.c $(BINDIR)/sljitLir.o -o $@ -lm -lpthread $(EXTRA_LIBS)

$(BINDIR)/loop: $(EXAMPLEDIR)/loop.c $(BINDIR)/.keep $(BINDIR)/sljitLir.o
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(EXAMPLEDIR)/loop.c $(BINDIR)/sljitLir.o -o $@ -lm -lpthread $(EXTRA_LIBS)
$(CC) $(CPPFLAGS) $(EXAMPLE_CFLAGS) $(LDFLAGS) $(EXAMPLEDIR)/loop.c $(BINDIR)/sljitLir.o -o $@ -lm -lpthread $(EXTRA_LIBS)

$(BINDIR)/array_access: $(EXAMPLEDIR)/array_access.c $(BINDIR)/.keep $(BINDIR)/sljitLir.o
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(EXAMPLEDIR)/array_access.c $(BINDIR)/sljitLir.o -o $@ -lm -lpthread $(EXTRA_LIBS)
$(CC) $(CPPFLAGS) $(EXAMPLE_CFLAGS) $(LDFLAGS) $(EXAMPLEDIR)/array_access.c $(BINDIR)/sljitLir.o -o $@ -lm -lpthread $(EXTRA_LIBS)

$(BINDIR)/func_call: $(EXAMPLEDIR)/func_call.c $(BINDIR)/.keep $(BINDIR)/sljitLir.o
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(EXAMPLEDIR)/func_call.c $(BINDIR)/sljitLir.o -o $@ -lm -lpthread $(EXTRA_LIBS)
$(CC) $(CPPFLAGS) $(EXAMPLE_CFLAGS) $(LDFLAGS) $(EXAMPLEDIR)/func_call.c $(BINDIR)/sljitLir.o -o $@ -lm -lpthread $(EXTRA_LIBS)

$(BINDIR)/struct_access: $(EXAMPLEDIR)/struct_access.c $(BINDIR)/.keep $(BINDIR)/sljitLir.o
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(EXAMPLEDIR)/struct_access.c $(BINDIR)/sljitLir.o -o $@ -lm -lpthread $(EXTRA_LIBS)
$(CC) $(CPPFLAGS) $(EXAMPLE_CFLAGS) $(LDFLAGS) $(EXAMPLEDIR)/struct_access.c $(BINDIR)/sljitLir.o -o $@ -lm -lpthread $(EXTRA_LIBS)

$(BINDIR)/temp_var: $(EXAMPLEDIR)/temp_var.c $(BINDIR)/.keep $(BINDIR)/sljitLir.o
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(EXAMPLEDIR)/temp_var.c $(BINDIR)/sljitLir.o -o $@ -lm -lpthread $(EXTRA_LIBS)
$(CC) $(CPPFLAGS) $(EXAMPLE_CFLAGS) $(LDFLAGS) $(EXAMPLEDIR)/temp_var.c $(BINDIR)/sljitLir.o -o $@ -lm -lpthread $(EXTRA_LIBS)

$(BINDIR)/brainfuck: $(EXAMPLEDIR)/brainfuck.c $(BINDIR)/.keep $(BINDIR)/sljitLir.o
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(EXAMPLEDIR)/brainfuck.c $(BINDIR)/sljitLir.o -o $@ -lm -lpthread $(EXTRA_LIBS)
$(CC) $(CPPFLAGS) $(EXAMPLE_CFLAGS) $(LDFLAGS) $(EXAMPLEDIR)/brainfuck.c $(BINDIR)/sljitLir.o -o $@ -lm -lpthread $(EXTRA_LIBS)
28 changes: 14 additions & 14 deletions doc/tutorial/array_access.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,39 @@
#include <stdio.h>
#include <stdlib.h>

typedef long (SLJIT_FUNC *func_arr_t)(long *arr, long narr);
typedef sljit_sw (SLJIT_FUNC *func_arr_t)(sljit_sw *arr, sljit_sw narr);

static void SLJIT_FUNC print_num(long a)
static void SLJIT_FUNC print_num(sljit_sw a)
{
printf("num = %ld\n", a);
printf("num = %ld\n", (long)a);
}

/*
This example, we generate a function like this:
long func(long *array, long narray)
sljit_sw func(sljit_sw *array, sljit_sw narray)
{
long i;
sljit_sw i;
for (i = 0; i < narray; ++i)
print_num(array[i]);
return narray;
}
*/

static int array_access(long *arr, long narr)
static int array_access(sljit_sw *arr, sljit_sw narr)
{
void *code;
size_t len;
sljit_uw len;
func_arr_t func;
struct sljit_label *loopstart;
struct sljit_jump *out;

/* Create a SLJIT compiler */
struct sljit_compiler *C = sljit_create_compiler(NULL, NULL);
struct sljit_compiler *C = sljit_create_compiler(NULL);

sljit_emit_enter(C, 0, SLJIT_ARGS2(W, P, W), 1, 3, 0, 0, 0);
/* opt arg R S FR FS local_size */
sljit_emit_enter(C, 0, SLJIT_ARGS2(W, P, W), 1, 3, 0);
/* opt arg R S local_size */

/* S2 = 0 */
sljit_emit_op2(C, SLJIT_XOR, SLJIT_S2, 0, SLJIT_S2, 0, SLJIT_S2, 0);
Expand All @@ -49,7 +49,7 @@ static int array_access(long *arr, long narr)
/* S2 >= narr --> jumo out */
out = sljit_emit_cmp(C, SLJIT_GREATER_EQUAL, SLJIT_S2, 0, SLJIT_S1, 0);

/* R0 = (long *)S0[S2]; */
/* R0 = (sljit_sw *)S0[S2]; */
sljit_emit_op1(C, SLJIT_MOV, SLJIT_R0, 0, SLJIT_MEM2(SLJIT_S0, SLJIT_S2), SLJIT_WORD_SHIFT);

/* print_num(R0) */
Expand All @@ -68,12 +68,12 @@ static int array_access(long *arr, long narr)
sljit_emit_return(C, SLJIT_MOV, SLJIT_S1, 0);

/* Generate machine code */
code = sljit_generate_code(C);
code = sljit_generate_code(C, 0, NULL);
len = sljit_get_generated_code_size(C);

/* Execute code */
func = (func_arr_t)code;
printf("func return %ld\n", func(arr, narr));
printf("func return %ld\n", (long)func(arr, narr));

/* dump_code(code, len); */

Expand All @@ -85,6 +85,6 @@ static int array_access(long *arr, long narr)

int main(void)
{
long arr[8] = { 3, -10, 4, 6, 8, 12, 2000, 0 };
sljit_sw arr[8] = { 3, -10, 4, 6, 8, 12, 2000, 0 };
return array_access(arr, 8);
}
16 changes: 8 additions & 8 deletions doc/tutorial/brainfuck.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ static void *SLJIT_FUNC my_alloc(size_t size, size_t n)
return calloc(size, n);
}

static void SLJIT_FUNC my_putchar(long c)
static void SLJIT_FUNC my_putchar(sljit_sw c)
{
putchar(c);
putchar((int)c);
}

static long SLJIT_FUNC my_getchar(void)
static sljit_sw SLJIT_FUNC my_getchar(void)
{
return getchar();
}
Expand All @@ -134,22 +134,22 @@ static void SLJIT_FUNC my_free(void *mem)
#define loop_empty() (loop_sp == 0)

/* compile bf source to a void func() */
static void *compile(FILE *src, unsigned long *lcode)
static void *compile(FILE *src, sljit_uw *lcode)
{
void *code = NULL;
int chr;
int nchr;

struct sljit_compiler *C = sljit_create_compiler(NULL, NULL);
struct sljit_compiler *C = sljit_create_compiler(NULL);
struct sljit_jump *end;
struct sljit_label *loop_start;
struct sljit_jump *loop_end;

int SP = SLJIT_S0; /* bf SP */
int CELLS = SLJIT_S1; /* bf array */

sljit_emit_enter(C, 0, SLJIT_ARGS2V(W, W), 2, 2, 0, 0, 0);
/* opt arg R S FR FS local_size */
sljit_emit_enter(C, 0, SLJIT_ARGS2V(W, W), 2, 2, 0);
/* opt arg R S local_size */

/* SP = 0 */
sljit_emit_op2(C, SLJIT_XOR, SP, 0, SP, 0, SP, 0);
Expand Down Expand Up @@ -217,7 +217,7 @@ static void *compile(FILE *src, unsigned long *lcode)
sljit_set_label(end, sljit_emit_label(C));
sljit_emit_return_void(C);

code = sljit_generate_code(C);
code = sljit_generate_code(C, 0, NULL);
if (lcode)
*lcode = sljit_get_generated_code_size(C);

Expand Down
16 changes: 8 additions & 8 deletions doc/tutorial/branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@
#include <stdio.h>
#include <stdlib.h>

typedef long (SLJIT_FUNC *func3_t)(long a, long b, long c);
typedef sljit_sw (SLJIT_FUNC *func3_t)(sljit_sw a, sljit_sw b, sljit_sw c);

/*
This example, we generate a function like this:
long func(long a, long b, long c)
sljit_sw func(sljit_sw a, sljit_sw b, sljit_sw c)
{
if ((a & 1) == 0)
return c;
return b;
}
*/
static int branch(long a, long b, long c)
static int branch(sljit_sw a, sljit_sw b, sljit_sw c)
{
void *code;
unsigned long len;
sljit_uw len;
func3_t func;

struct sljit_jump *ret_c;
struct sljit_jump *out;

/* Create a SLJIT compiler */
struct sljit_compiler *C = sljit_create_compiler(NULL, NULL);
struct sljit_compiler *C = sljit_create_compiler(NULL);

/* 3 arg, 1 temp reg, 3 save reg */
sljit_emit_enter(C, 0, SLJIT_ARGS3(W, W, W, W), 1, 3, 0, 0, 0);
sljit_emit_enter(C, 0, SLJIT_ARGS3(W, W, W, W), 1, 3, 0);

/* R0 = a & 1, S0 is argument a */
sljit_emit_op2(C, SLJIT_AND, SLJIT_R0, 0, SLJIT_S0, 0, SLJIT_IMM, 1);
Expand All @@ -56,12 +56,12 @@ static int branch(long a, long b, long c)
sljit_emit_return(C, SLJIT_MOV, SLJIT_RETURN_REG, 0);

/* Generate machine code */
code = sljit_generate_code(C);
code = sljit_generate_code(C, 0, NULL);
len = sljit_get_generated_code_size(C);

/* Execute code */
func = (func3_t)code;
printf("func return %ld\n", func(a, b, c));
printf("func return %ld\n", (long)func(a, b, c));

/* dump_code(code, len); */

Expand Down
14 changes: 7 additions & 7 deletions doc/tutorial/first_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
#include <stdio.h>
#include <stdlib.h>

typedef long (SLJIT_FUNC *func3_t)(long a, long b, long c);
typedef sljit_sw (SLJIT_FUNC *func3_t)(sljit_sw a, sljit_sw b, sljit_sw c);

static int add3(long a, long b, long c)
static int add3(sljit_sw a, sljit_sw b, sljit_sw c)
{
void *code;
unsigned long len;
sljit_uw len;
func3_t func;

/* Create a SLJIT compiler */
struct sljit_compiler *C = sljit_create_compiler(NULL, NULL);
struct sljit_compiler *C = sljit_create_compiler(NULL);

/* Start a context(function entry), have 3 arguments, discuss later */
sljit_emit_enter(C, 0, SLJIT_ARGS3(W, W, W, W), 1, 3, 0, 0, 0);
sljit_emit_enter(C, 0, SLJIT_ARGS3(W, W, W, W), 1, 3, 0);

/* The first arguments of function is register SLJIT_S0, 2nd, SLJIT_S1, etc. */
/* R0 = first */
Expand All @@ -32,12 +32,12 @@ static int add3(long a, long b, long c)
sljit_emit_return(C, SLJIT_MOV, SLJIT_R0, 0);

/* Generate machine code */
code = sljit_generate_code(C);
code = sljit_generate_code(C, 0, NULL);
len = sljit_get_generated_code_size(C);

/* Execute code */
func = (func3_t)code;
printf("func return %ld\n", func(a, b, c));
printf("func return %ld\n", (long)func(a, b, c));

/* dump_code(code, len); */

Expand Down
20 changes: 10 additions & 10 deletions doc/tutorial/func_call.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,38 @@
#include <stdio.h>
#include <stdlib.h>

typedef long (SLJIT_FUNC *func3_t)(long a, long b, long c);
typedef sljit_sw (SLJIT_FUNC *func3_t)(sljit_sw a, sljit_sw b, sljit_sw c);

static long SLJIT_FUNC print_num(long a)
static sljit_sw SLJIT_FUNC print_num(sljit_sw a)
{
printf("a = %ld\n", a);
printf("a = %ld\n", (long)a);
return a + 1;
}

/*
This example, we generate a function like this:
long func(long a, long b, long c)
sljit_sw func(sljit_sw a, sljit_sw b, sljit_sw c)
{
if ((a & 1) == 0)
return print_num(c);
return print_num(b);
}
*/

static int func_call(long a, long b, long c)
static int func_call(sljit_sw a, sljit_sw b, sljit_sw c)
{
void *code;
unsigned long len;
sljit_uw len;
func3_t func;

struct sljit_jump *out;
struct sljit_jump *print_c;

/* Create a SLJIT compiler */
struct sljit_compiler *C = sljit_create_compiler(NULL, NULL);
struct sljit_compiler *C = sljit_create_compiler(NULL);

sljit_emit_enter(C, 0, SLJIT_ARGS3(W, W, W, W), 3, 3, 0, 0, 0);
sljit_emit_enter(C, 0, SLJIT_ARGS3(W, W, W, W), 3, 3, 0);

/* a & 1 --> R0 */
sljit_emit_op2(C, SLJIT_AND, SLJIT_R0, 0, SLJIT_S0, 0, SLJIT_IMM, 1);
Expand All @@ -59,12 +59,12 @@ static int func_call(long a, long b, long c)
sljit_emit_return(C, SLJIT_MOV, SLJIT_R0, 0);

/* Generate machine code */
code = sljit_generate_code(C);
code = sljit_generate_code(C, 0, NULL);
len = sljit_get_generated_code_size(C);

/* Execute code */
func = (func3_t)code;
printf("func return %ld\n", func(a, b, c));
printf("func return %ld\n", (long)func(a, b, c));

/* dump_code(code, len); */

Expand Down
20 changes: 10 additions & 10 deletions doc/tutorial/loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,36 @@
#include <stdio.h>
#include <stdlib.h>

typedef long (SLJIT_FUNC *func2_t)(long a, long b);
typedef sljit_sw (SLJIT_FUNC *func2_t)(sljit_sw a, sljit_sw b);

/*
This example, we generate a function like this:
long func(long a, long b)
sljit_sw func(sljit_sw a, sljit_sw b)
{
long i;
long ret = 0;
sljit_sw i;
sljit_sw ret = 0;
for (i = 0; i < a; ++i) {
ret += b;
}
return ret;
}
*/

static int loop(long a, long b)
static int loop(sljit_sw a, sljit_sw b)
{
void *code;
unsigned long len;
sljit_uw len;
func2_t func;

struct sljit_label *loopstart;
struct sljit_jump *out;

/* Create a SLJIT compiler */
struct sljit_compiler *C = sljit_create_compiler(NULL, NULL);
struct sljit_compiler *C = sljit_create_compiler(NULL);

/* 2 arg, 2 temp reg, 2 saved reg */
sljit_emit_enter(C, 0, SLJIT_ARGS2(W, W, W), 2, 2, 0, 0, 0);
sljit_emit_enter(C, 0, SLJIT_ARGS2(W, W, W), 2, 2, 0);

/* R0 = 0 */
sljit_emit_op2(C, SLJIT_XOR, SLJIT_R1, 0, SLJIT_R1, 0, SLJIT_R1, 0);
Expand All @@ -55,12 +55,12 @@ static int loop(long a, long b)
sljit_emit_return(C, SLJIT_MOV, SLJIT_RETURN_REG, 0);

/* Generate machine code */
code = sljit_generate_code(C);
code = sljit_generate_code(C, 0, NULL);
len = sljit_get_generated_code_size(C);

/* Execute code */
func = (func2_t)code;
printf("func return %ld\n", func(a, b));
printf("func return %ld\n", (long)func(a, b));

/* dump_code(code, len); */

Expand Down
Loading

0 comments on commit 7a6ceb3

Please sign in to comment.