Parcourir la source

tripl: 0.1.0-alpha1: supporting on x86_64 and use the new (0.1.0) version

rain il y a 4 ans
Parent
commit
5fe8a64f96
12 fichiers modifiés avec 510 ajouts et 34 suppressions
  1. 1 1
      arch/Makefile
  2. 53 3
      arch/interp/interp.c
  3. 16 0
      arch/x86_64/Makefile
  4. 275 0
      arch/x86_64/asm.c
  5. 56 0
      arch/x86_64/do_keyword.c
  6. 1 0
      include/klist.h
  7. 5 0
      include/tripl/arch.h
  8. 12 2
      include/tripl/tripl.h
  9. 1 0
      klist.c
  10. 9 11
      man/tripl.1
  11. 6 1
      sample.trp
  12. 75 16
      tripl.c

+ 1 - 1
arch/Makefile

@@ -1,4 +1,4 @@
-arch = interp 
+arch = interp x86_64 
 
 
 
 
 all : 
 all : 

+ 53 - 3
arch/interp/interp.c

@@ -42,6 +42,8 @@ static int do_funcall(char *p),
 	   do_dl_funcall(void *f, char *p), 
 	   do_dl_funcall(void *f, char *p), 
 	   do_check_string(char *p, char *d);
 	   do_check_string(char *p, char *d);
 
 
+static inline struct klist_node *find_name(char *s);
+
 extern int arch_funcall(void *f, struct arg_entry *p, int nargs);
 extern int arch_funcall(void *f, struct arg_entry *p, int nargs);
 
 
 
 
@@ -55,8 +57,9 @@ int interp_init(void) {
 
 
 int interp_do_keyword(char *p, char c) {
 int interp_do_keyword(char *p, char c) {
 	struct klist_node *n;
 	struct klist_node *n;
-	int type, out, i;
+	int type, out, i, attrs;
 
 
+	char *np;
 	char *name = NULL;
 	char *name = NULL;
 	void *dlib;
 	void *dlib;
 
 
@@ -84,7 +87,11 @@ int interp_do_keyword(char *p, char c) {
 				exit(generic_error("'$' requires a number", lbsave, lno));
 				exit(generic_error("'$' requires a number", lbsave, lno));
 		}
 		}
 
 
-		push_nr(out, name);
+		p++;
+		_skip_word(p);
+
+		attrs = getattrmask(p, vardef_attrtab);
+		push_nr(out, name, attrs);
 
 
 		break;
 		break;
 
 
@@ -157,6 +164,49 @@ int interp_do_keyword(char *p, char c) {
 		/* function call. the real funny thing is going on :-) */
 		/* function call. the real funny thing is going on :-) */
 		do_funcall(p);
 		do_funcall(p);
 
 
+		break;
+
+	default:
+		/* setting 'i = 0' */
+		getword(p, &type, &out);
+
+		if (type != WORDTYPE_NAME) 
+			exit(generic_error("unknow keyword", lbsave, lno));
+
+		name = np = strndup(p, out);
+
+		if (name[0] == '&') 
+			name++;
+
+		if (!(n = find_name(name))) 
+			exit(generic_error("name not found", lbsave, lno));
+
+		if (n->xmask & 4)
+			/* read only */
+			exit(generic_error("variable sets attribute 'read-only'", lbsave, lno));
+
+		if (n->xmask & 1 && np == name) 
+			exit(generic_error("no '&' found before global variable", lbsave, lno));
+
+		_skip_word(p);
+		_skip_spaces(p);
+
+		if (!getword(p, &type, &out)) 
+			exit(generic_error("no equal", lbsave, lno));
+
+		if (out != '=') 
+			exit(generic_error("not a '='", lbsave, lno));
+
+		p++;
+		_skip_spaces(p);
+
+		if (!getword(p, &type, &out)) 
+			exit(generic_error("no value", lbsave, lno));
+
+		n->value = out;
+
+		free(np);
+
 		break;
 		break;
 	}
 	}
 
 
@@ -284,7 +334,7 @@ static int do_dl_funcall(void *f, char *p) {
 		}
 		}
 
 
 		else 
 		else 
-			p += out + 1;
+			p += out + 2;
 
 
 		++n;
 		++n;
 	}
 	}

+ 16 - 0
arch/x86_64/Makefile

@@ -0,0 +1,16 @@
+objs = do_keyword.o asm.o 
+targ = ../libarch.a 
+CFLAGS = -Wall -Werror -I../../include -g -ldl 
+
+all : $(objs)
+	@$(AR) cr -ldl $(targ) $(objs) 
+
+
+%.o : %.c 
+	@$(CC) -c $*.c $(CFLAGS)
+	@echo -e "\tCC\t\t\t$*.c"
+
+
+.PHONY clean :
+	$(RM) $(objs)
+

+ 275 - 0
arch/x86_64/asm.c

@@ -0,0 +1,275 @@
+/* tripl/arch/x86_64/asm.c 
+ * 
+ * This file is part of tripl. 
+ * 
+ * tripl is free software: you can redistribute it and/or modify 
+ * it under the terms of the GNU General Public License as published by 
+ * the Free Software Foundation, either version 3 of the License, or 
+ * (at your option) any later version. 
+ * 
+ * tripl is distributed in the hope that it will be useful, 
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of 
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+ * GNU General Public License for more details. 
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with tripl. If not, see <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+#include <stdio.h>
+#include <string.h>
+
+#include <klist.h>
+#include <tripl/tripl.h>
+#include <tripl/limits.h>
+#include <tripl/arch.h>
+
+
+static char *lbsave;
+static int stack_index;
+
+static int do_funcall(char *p);
+static int set_name(char *p, char *name);
+
+
+const static char asm_head[] = {
+	"	.file \"%s\"\n"
+	"	.text\n"
+
+	"	.globl 	_start\n"
+	"_start:\n"
+	"	/* call main and exit */\n"
+	"	call 	main\n"
+	"	movq 	%%rax, %%rbx\n"
+	"	movq 	$1, %%rax\n"
+	"	/* exit */\n"
+	"	int 	$0x80\n\n"
+
+	"	.globl 	main\n"
+	/* use -x(%rbp) to access */
+	"main:	pushq 	%%rbp\n"
+	"	movq 	%%rsp, %%rbp\n"
+};
+
+
+const static char global_var[] = {
+	"	.data\n"
+	"	.globl 	%s\n"
+	"%s: 	.long 	%d\n"
+	"	.text\n"
+};
+
+
+const static char stack_var[] = {
+	"	movq 	$%d, -%d(%%rbp)\n"
+};
+
+
+const static char wr_stkvar[] = {
+	"	movq 	$%d, -%d(%%rbp)\n"
+};
+
+
+const static char wr_glbvar[] = {
+	"	movq 	$%d, %s(%%rip)\n"
+};
+
+
+const static char noarg_funcall[] = {
+	"	call	%s\n"
+};
+
+
+const static char funcend[] = {
+	"	popq 	%%rbp\n"
+	"	ret\n"
+};
+
+
+int x86_64_init_asm(void) {
+	stack_index = 0;
+	printf(asm_head, fname);
+
+	return 0;
+}
+
+
+int x86_64_asm_fin(void) {
+	printf(funcend);
+
+	return 0;
+}
+
+
+int x86_64_do_asm(char *p, char c) {
+	char *name = NULL;
+	int out, type, attr;
+	lbsave = p;
+
+	switch (c) {
+	case '$':
+		++p;
+		_skip_spaces(p);
+
+		getword(p, &type, &out);
+
+		if (type != WORDTYPE_NAME) 
+			exit(generic_error("'$' requires a name", lbsave, lno));
+
+		name = strndup(p, out);
+		p += out;
+
+		getword(p, &type, &out);
+
+		if (type != WORDTYPE_NR) 
+			exit(generic_error("'$' requires a number", lbsave, lno));
+
+		_skip_spaces(p);
+		_skip_word(p);
+		_skip_spaces(p);
+
+		attr = getattrmask(p, vardef_attrtab);
+
+		if (attr & 1) 
+			/* global variable */
+			printf(global_var, name, name, out);
+
+		else {
+			/* variables on stack */
+			printf(stack_var, out, stack_index << 3);
+			push_nr(stack_index, name, attr);
+
+			stack_index++;
+		}
+
+		break;
+
+	case '@':
+		p++;
+		_skip_spaces(p);
+
+		do_funcall(p);
+
+		break;
+
+	default:
+		/* setting */
+		getword(p, &type, &out);
+
+		if (type != WORDTYPE_NAME) 
+			exit(generic_error("unknow keyword", lbsave, lno));
+
+		name = strndup(p, out);
+
+		set_name(p, name);
+
+		free(name);
+
+		break;
+	}
+
+	return 0;
+}
+
+
+static inline struct klist_node *find_name_stack(char *name) {
+	struct klist_node *n;
+
+	for (n = stack.next; n != &stack; n = n->next) 
+		if (!strcmp(n->payload, name)) 
+			return n;
+
+	return NULL;
+}
+
+
+static int set_name(char *p, char *name) {
+	int type, out;
+	struct klist_node *n;
+
+	/* find the name, 
+	 * first find in stack */
+
+	if ((n = find_name_stack(name))) {
+		if (n->xmask & 4) 
+			exit(generic_error("variable sets attribute 'read-only'", lbsave, lno));
+
+		_skip_word(p);
+		_skip_spaces(p);
+
+		if (!getword(p, &type, &out)) 
+			exit(generic_error("no equal", lbsave, lno));
+
+		if (out != '=') 
+			exit(generic_error("not a '='", lbsave, lno));
+
+		p++;
+		_skip_spaces(p);
+
+		if (!getword(p, &type, &out)) 
+			exit(generic_error("no value", lbsave, lno));
+
+
+		printf(wr_stkvar, out, n->value << 3);
+
+		return 0;
+	}
+
+	/* global variables */
+
+	if (name[0] != '&') 
+		/* we need a '&' for global variables */
+		exit(generic_error("name not found (did you forget '&')", lbsave, lno));
+
+	_skip_word(p);
+	_skip_spaces(p);
+
+	if (!getword(p, &type, &out)) 
+		exit(generic_error("no equal", lbsave, lno));
+
+	if (out != '=') 
+		exit(generic_error("not a '='", lbsave, lno));
+
+	p++;
+	_skip_spaces(p);
+
+	if (!getword(p, &type, &out)) 
+		exit(generic_error("no value", lbsave, lno));
+
+	printf(wr_glbvar, out, name + 1);
+
+	return 0;
+}
+
+
+int do_funcall(char *p) {
+//	struct arg_entry *args, *next_arg;
+	int out, type;
+	char *funcname;
+
+	getword(p, &type, &out);
+
+	if (type != WORDTYPE_NAME) 
+		exit(generic_error("a function name required", lbsave, lno));
+
+//	args = malloc(sizeof(struct arg_entry));
+//	next_arg = args;
+
+	funcname = strndup(p, out);
+	p += out;
+
+	if (*p == ';') {
+		printf(noarg_funcall, funcname);
+
+		return 0;
+	}
+
+	free(funcname);
+
+	return 0;
+}
+
+

+ 56 - 0
arch/x86_64/do_keyword.c

@@ -0,0 +1,56 @@
+/* tripl/arch/x86_64/do_keyword.c 
+ * 
+ * This file is part of tripl. 
+ * 
+ * tripl is free software: you can redistribute it and/or modify 
+ * it under the terms of the GNU General Public License as published by 
+ * the Free Software Foundation, either version 3 of the License, or 
+ * (at your option) any later version. 
+ * 
+ * tripl is distributed in the hope that it will be useful, 
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of 
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+ * GNU General Public License for more details. 
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with tripl. If not, see <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+/* gas x86_64 assembly, to stdout */
+
+#include <stdio.h>
+
+#include <klist.h>
+#include <tripl/tripl.h>
+#include <tripl/limits.h>
+#include <tripl/arch.h>
+
+
+extern int x86_64_init_asm(void);
+extern int x86_64_do_asm(char *s, char c);
+extern int x86_64_asm_fin(void);
+
+
+int x86_64_init(void) {
+	klist_init(&stack);
+
+	return x86_64_init_asm();
+}
+
+
+int x86_64_do_keyword(char *s, char c) {
+	x86_64_do_asm(s, c);
+
+	return 0;
+}
+
+int x86_64_fin(void) {
+	x86_64_asm_fin();
+
+	return 0;
+}
+
+

+ 1 - 0
include/klist.h

@@ -27,6 +27,7 @@
 
 
 struct klist_node {
 struct klist_node {
 	int value;
 	int value;
+	int xmask;
 	void *payload;
 	void *payload;
 
 
 	struct klist_node *next, *prev;
 	struct klist_node *next, *prev;

+ 5 - 0
include/tripl/arch.h

@@ -26,6 +26,11 @@
 #include <tripl/tripl.h>
 #include <tripl/tripl.h>
 
 
 int interp_do_keyword(char *s, char c);
 int interp_do_keyword(char *s, char c);
+int interp_init(void);
+
+int x86_64_do_keyword(char *s, char c);
+int x86_64_init(void);
+int x86_64_fin(void);
 
 
 
 
 #endif
 #endif

+ 12 - 2
include/tripl/tripl.h

@@ -73,7 +73,7 @@
 #define ischr(c) 	(((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
 #define ischr(c) 	(((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
 #define _skip_spaces(s) while (isspace(*(s))) (s)++;
 #define _skip_spaces(s) while (isspace(*(s))) (s)++;
 #define skip_spaces() 	_skip_spaces(line_buffer);
 #define skip_spaces() 	_skip_spaces(line_buffer);
-#define _skip_word(s) 	{ while (!isspace(*(s))) (s)++; _skip_spaces(s); }
+#define _skip_word(s) 	while (!isspace(*(s))) (s)++; _skip_spaces(s)
 
 
 /* okay, it's a wrong thing itoa, should be atoi... */
 /* okay, it's a wrong thing itoa, should be atoi... */
 #define itoa(c) 	((c) - '0')
 #define itoa(c) 	((c) - '0')
@@ -81,11 +81,12 @@
 #define atoX(c) 	((c) >= 'A' ? (c) - 'A' + 10 : (c) - '0')
 #define atoX(c) 	((c) >= 'A' ? (c) - 'A' + 10 : (c) - '0')
 
 
 /* warning that np must be allocated by malloc or 0, not on stack */
 /* warning that np must be allocated by malloc or 0, not on stack */
-#define push_nr(nr, np) ({ \
+#define push_nr(nr, np, mask) ({ \
 	struct klist_node *p = malloc(sizeof(struct klist_node)); \
 	struct klist_node *p = malloc(sizeof(struct klist_node)); \
 	klist_init(p); \
 	klist_init(p); \
 	p->value = nr; \
 	p->value = nr; \
 	p->payload = np; \
 	p->payload = np; \
+	p->xmask = mask; \
 	klist_add(&stack, p); \
 	klist_add(&stack, p); \
 })
 })
 
 
@@ -125,9 +126,18 @@ struct arg_entry {
 };
 };
 
 
 
 
+struct attr_table {
+	char 			*attrname;
+	int 			attrmask;
+};
+
+extern const struct attr_table vardef_attrtab[];
+
+
 int do_tripl_compile(char *, off_t);
 int do_tripl_compile(char *, off_t);
 
 
 char *getword(char *fbuffer, int *type, int *out);
 char *getword(char *fbuffer, int *type, int *out);
+int getattrmask(char *p, const struct attr_table *tab);
 int generic_error(char *msg, char *line, int lno);
 int generic_error(char *msg, char *line, int lno);
 
 
 
 

+ 1 - 0
klist.c

@@ -36,6 +36,7 @@ struct klist_node *klist_find_value(struct klist_node *head, int value) {
 
 
 
 
 void klist_init(struct klist_node *head) {
 void klist_init(struct klist_node *head) {
+	head->xmask = 0;
 	head->next = head;
 	head->next = head;
 	head->prev = head;
 	head->prev = head;
 	head->value = 0;
 	head->value = 0;

+ 9 - 11
man/tripl.1

@@ -17,29 +17,27 @@ that.
 .SH SYNTAX
 .SH SYNTAX
 \fB$\fP: uses to push a number into stack. 
 \fB$\fP: uses to push a number into stack. 
 
 
-eg. 	\fB$ 1048576\fP
+eg. 	\fB$ name 1048576\fP
 
 
 	\fB$ -1048576\fP
 	\fB$ -1048576\fP
 
 
 	\fB$ 0\fP
 	\fB$ 0\fP
 
 
-\fB#\fP: line comment
+\fB`\fP: pop a number and free the memory of it. 
+
+eg. 	\fB`\fP
 
 
-\fB+\fP: addition. This will add the numbers of the top stack (pop them 
-out) and push the result into stack. 
+\fB#\fP: line comment
 
 
 \fB!\fP: stack dump. This dumps the stack, you can see numbers in the stack, 
 \fB!\fP: stack dump. This dumps the stack, you can see numbers in the stack, 
 but this won't change anything. 
 but this won't change anything. 
 
 
-\fB~\fP: print out message. This prints a message: 
-
-eg. 	\fB~ hello, world\fP
+\fB<\fP: import shared library. 
 
 
-	\fB~ hello, world\\n\fP
+eg. 	\fB< foobar.so\fP
 
 
-\fB:\fP: print a number. 
+\fB@\fP: calling function. \fBWARNING: MAX 6 ARGUMENTS SUPPORT\fP
 
 
-eg. 	\fB: 1\fP	: prints stack[0] as number
+eg. 	\fB@ printf "hello, world %d %s" 0 "hello"
 
 
-	\fB: 2\fP 	: prints stack[1] as number
 
 

+ 6 - 1
sample.trp

@@ -1,3 +1,8 @@
+$ age 12;
+!;
+
 < "libc-2.33.so";
 < "libc-2.33.so";
-@ printf "a: %d %d + %d\n" 10 1 1 2;
+@ printf "hello, world\n";
+@ printf "my age: %d\n" age;
+
 
 

+ 75 - 16
tripl.c

@@ -35,41 +35,55 @@ char *linebuffer_save = NULL;
 char *line_buffer = NULL;
 char *line_buffer = NULL;
 int lno = 0;
 int lno = 0;
 char *eof;
 char *eof;
-
 char *line_eof;
 char *line_eof;
 
 
 struct klist_node stack;
 struct klist_node stack;
 
 
-
-extern int interp_init(void);
-
 static int next_line(char *), 
 static int next_line(char *), 
 	   parse_line(void);
 	   parse_line(void);
 
 
 int (* do_keyword) (char *, char);
 int (* do_keyword) (char *, char);
+int (* finish) (void);
 
 
 const static struct {
 const static struct {
 	char *name;
 	char *name;
 	int (* do_keyword) (char *, char);
 	int (* do_keyword) (char *, char);
+	int (* init) (void);
+	int (* fin) (void);
 } arch_list[] = {
 } arch_list[] = {
-	{ "interp", interp_do_keyword }, 
+	{ "interp", interp_do_keyword, interp_init, NULL }, 
+	{ "x86_64", x86_64_do_keyword, x86_64_init, x86_64_fin }, 
 	{ NULL, NULL }
 	{ NULL, NULL }
 };
 };
 
 
 
 
+const struct attr_table vardef_attrtab[] = {
+	{ "global", 1 }, 
+	{ "rdonly", 4 }, 
+	{ "ptr", 8 }, 
+	{ NULL, 0 }
+};
+
+
 
 
 int do_tripl_compile(char *fbuffer, off_t fsize) {
 int do_tripl_compile(char *fbuffer, off_t fsize) {
 	char *arch = getenv("TRIPL_ARCH");
 	char *arch = getenv("TRIPL_ARCH");
 	int i;
 	int i;
 
 
 
 
-	if (!arch) 
+	if (!arch) {
 		do_keyword = interp_do_keyword;
 		do_keyword = interp_do_keyword;
 
 
+		interp_init();
+	}
+
 	else {
 	else {
 		for (i = 0; arch_list[i].name; i++) 
 		for (i = 0; arch_list[i].name; i++) 
 			if (!strcmp(arch_list[i].name, arch)) {
 			if (!strcmp(arch_list[i].name, arch)) {
 				do_keyword = arch_list[i].do_keyword;
 				do_keyword = arch_list[i].do_keyword;
+				arch_list[i].init();
+
+				finish = arch_list[i].fin;
 
 
 				goto ok;
 				goto ok;
 			}
 			}
@@ -79,17 +93,15 @@ int do_tripl_compile(char *fbuffer, off_t fsize) {
 		exit(1);
 		exit(1);
 	}
 	}
 
 
-ok:	if (do_keyword == interp_do_keyword) 
-		interp_init();
-
-	klist_init(&stack);
+ok:	klist_init(&stack);
 
 
 	eof = fbuffer + fsize;
 	eof = fbuffer + fsize;
 
 
 	while (next_line(fbuffer)) 
 	while (next_line(fbuffer)) 
-		if (parse_line()) 
-			/* don't flush streams */
-			_exit(1);
+		parse_line();
+
+	if (finish) 
+		finish();
 
 
 	return 0;
 	return 0;
 }
 }
@@ -125,7 +137,7 @@ static int next_line(char *fbuffer) {
 
 
 	if (p == eof) 
 	if (p == eof) 
 		/* eof */
 		/* eof */
-		exit(1);
+		return 0;
 
 
 	linebuffer_save = line_buffer;
 	linebuffer_save = line_buffer;
 
 
@@ -222,7 +234,7 @@ char *getword(char *fbuffer, int *type, int *out) {
 					*type = WORDTYPE_NAME;
 					*type = WORDTYPE_NAME;
 					*out = 1;
 					*out = 1;
 
 
-					return p;
+					return --p;
 				}
 				}
 
 
 				/* keyword */
 				/* keyword */
@@ -275,6 +287,53 @@ char *getword(char *fbuffer, int *type, int *out) {
 }
 }
 
 
 
 
+static inline int find_attr(char *attrname, const struct attr_table *tab) {
+	int i;
+
+	for (i = 0; tab[i].attrname; i++) 
+		if (!strcmp(tab[i].attrname, attrname)) 
+			return tab[i].attrmask;
+
+	return 0;
+}
+
+
+int getattrmask(char *p, const struct attr_table *tab) {
+	char *attrname;
+	int type, out, mask = 0, t;
+
+	if (!getword(p, &type, &out)) 
+		return 0;
+
+	if (type != WORDTYPE_KWORD) 
+		exit(generic_error("non-attribute ('@') character after that", linebuffer_save, lno));
+
+	if (out != '@') 
+		exit(generic_error("not a '@' after string", linebuffer_save, lno));
+
+	++p;
+	_skip_spaces(p);
+
+	while (getword(p, &type, &out)) {
+		if (type != WORDTYPE_NAME) 
+			exit(generic_error("an attribute name required", linebuffer_save, lno));
+
+		attrname = strndup(p, out);
+
+		if (!(t = find_attr(attrname, tab))) 
+			exit(generic_error("unknow attribute", linebuffer_save, lno));
+
+		mask |= t;
+
+		free(attrname);
+		_skip_word(p);
+	}
+
+	return mask;
+}
+
+
+
 static int parse_line(void) {
 static int parse_line(void) {
 	char *p;
 	char *p;
 	int type, out;
 	int type, out;
@@ -284,7 +343,7 @@ static int parse_line(void) {
 		/* empty line */
 		/* empty line */
 		return 0;
 		return 0;
 
 
-	if (type != WORDTYPE_KWORD) 
+	if (type != WORDTYPE_KWORD && type != WORDTYPE_NAME) 
 		exit(generic_error("line starts with a non-keyword", linebuffer_save, lno));
 		exit(generic_error("line starts with a non-keyword", linebuffer_save, lno));
 
 
 	return do_keyword(p, *p);
 	return do_keyword(p, *p);