Ver código fonte

tripl: support math expressions with no prio on real machines, only add and sub

intirain 4 anos atrás
pai
commit
bab0751049
11 arquivos alterados com 254 adições e 85 exclusões
  1. 5 4
      Makefile
  2. 2 1
      arch/Makefile
  3. 57 36
      arch/arch_templ.c
  4. 1 1
      arch/x86_64/Makefile
  5. 3 2
      arch/x86_64/asm.c
  6. 3 1
      arch/x86_64/x86_64.c
  7. 176 0
      arch/x86_64/x86_64_math.c
  8. 1 0
      include/config.h
  9. 1 1
      include/tripl/arch.h
  10. 1 0
      main.c
  11. 4 39
      sample.trp

+ 5 - 4
Makefile

@@ -1,6 +1,7 @@
 tripl-objs 	= main.o tripl.o error.o klist.o  
 tripl-out 	= tripl
 
+MAKEFLAGS 	= --no-print-directory 
 CFLAGS 		= -Wall -Werror -Iinclude -g -ldl 
 
 ifeq ($(install-dir),)
@@ -8,8 +9,11 @@ ifeq ($(install-dir),)
 endif
 
 
-all : $(tripl-objs) 
+all : 
 	@scripts/mkconfig.py 
+	@$(MAKE) _real_all
+
+_real_all : $(tripl-objs)
 	@$(MAKE) -C arch/ 
 	@$(CC) 	$(tripl-objs) -o $(tripl-out) -Larch -larch -ldl 
 	@echo "tripl is ready. "
@@ -26,6 +30,3 @@ install : $(tripl-out)
 	@$(RM) 	-f $(tripl-objs) $(tripl-out)
 	@$(MAKE) -C arch/ clean
 
-startbeta:
-	touch "betatest-`date +\"%Y:%m:%d\"`"
-

+ 2 - 1
arch/Makefile

@@ -9,7 +9,8 @@ all : 	$(templ)
 	@for n in $(arch); do \
 		$(MAKE) -C $$n; \
 	done;
-	$(AR) cr $(targ) $(templ)
+	@$(AR) cr $(targ) $(templ)
+	@echo -e "\tAR\t\t\t$(targ)"
 
 
 %.o : %.c 

+ 57 - 36
arch/arch_templ.c

@@ -135,65 +135,86 @@ struct klist_node *find_name(char *name) {
 
 __arch_templ static int new_var(char *p, struct arch *arch_fn) {
 	char *name;
-	int type, out, attr;
+	int type, out, attr, comma = 0;
 	struct klist_node *t;
 	
-	getword(p, &type, &out);
+	while (getword(p, &type, &out)) {
+		if (unlikely(type != WORDTYPE_NAME)) 
+			exit(generic_error("a name for variable required", psave, lno));
 
-	if (unlikely(type != WORDTYPE_NAME)) 
-		exit(generic_error("a name for variable required", psave, lno));
+		name = strndup(p, out);
 
-	name = strndup(p, out);
+		p += out;
+		_skip_spaces(p);
 
-	p += out;
-	_skip_spaces(p);
+		getword(p, &type, &out);
 
-	getword(p, &type, &out);
+		switch (type) {
+		case WORDTYPE_KWORD:
+			if (out == ',') {
+				comma = 1;
 
-	switch (type) {
-	case WORDTYPE_KWORD:
-		if (unlikely(out != '=')) 
-			exit(generic_error("keyword but it's not a '='", psave, lno));
+				p++;
+				_skip_spaces(p);
+
+				break;
+			}
+
+			if (out == '@') {
+				out = 0;
+
+				break;
+			}
+
+			if (unlikely(out != '=' )) 
+				exit(generic_error("keyword but it's not a '='", psave, lno));
 		
-		++p;
-		_skip_spaces(p);
+			++p;
+			_skip_spaces(p);
 
-		getword(p, &type, &out);
+			getword(p, &type, &out);
 
-		if (unlikely(type != WORDTYPE_NR)) 
-			exit(generic_error("a number required", psave, lno));
+			if (unlikely(type != WORDTYPE_NR)) 
+				exit(generic_error("a number required", psave, lno));
 
-		_skip_word(p);
+			_skip_word(p);
 
-		break;
+			break;
 
-	case WORDTYPE_NR:
-		/* to attribute (if have) */
+		case WORDTYPE_NR:
+			/* to attribute (if have) */
 
-		_skip_word(p);
+			_skip_word(p);
 
-		break;
+			break;
 
-	case 0:
-		out = 0;
+		case 0:
+			out = 0;
 
-		break;
+			break;
 
-	default:
-		exit(generic_error("bad word after variable name", psave, lno));
-	}
+		default:
+			exit(generic_error("bad word after variable name", psave, lno));
+		}
 
-	attr = getattrmask(p, vardef_attrtab);
 
-	if (unlikely((t = find_name(name)) && !(t->xmask & XMASK_WEAK))) {
-		error(name);
+		if (!comma) 
+			attr = getattrmask(p, vardef_attrtab);
 
-		exit(generic_error("name already defined", psave, lno));
-	}
+		if (unlikely((t = find_name(name)) && !(t->xmask & XMASK_WEAK))) {
+			error(name);
 
-	arch_fn->arch_new_var(name, out, attr);
+			exit(generic_error("name already defined", psave, lno));
+		}
 
-	/* the name can't be free */
+		arch_fn->arch_new_var(name, out, attr);
+
+		if (!comma) 
+			break;
+
+		comma = 0;
+		_skip_spaces(p);
+	}
 
 	return 0;
 }

+ 1 - 1
arch/x86_64/Makefile

@@ -1,4 +1,4 @@
-objs = x86_64.o asm.o 
+objs = x86_64.o asm.o x86_64_math.o 
 targ = ../libarch.a 
 CFLAGS = -Wall -Werror -I../../include -g -ldl 
 

+ 3 - 2
arch/x86_64/asm.c

@@ -42,7 +42,7 @@ extern char *psave;
 
 
 __ll_asm const static char asm_head[] = {
-	"	.file \"%s\"\n"
+	"	.file 	\"%s\"\n"
 	"	.text\n"
 
 	"	.globl 	_start\n"
@@ -64,7 +64,8 @@ __ll_asm const static char asm_head[] = {
 __ll_asm const static char global_var[] = {
 	"	.data\n"
 	"	.globl 	%s\n"
-	"%s: 	.quad 	%ld\n"
+	"%s: \n"
+	"	.quad 	%ld\n"
 	"	.text\n"
 };
 

+ 3 - 1
arch/x86_64/x86_64.c

@@ -34,6 +34,7 @@ int x86_64_fin(void);
 
 int x86_64_new_var(char *name, long value, int attr);
 int x86_64_set_var(char *name, long value, int attr);
+int x86_64_math_expr(char *s, char *name);
 
 
 struct arch x86_64_arch_fn = {
@@ -43,7 +44,8 @@ struct arch x86_64_arch_fn = {
 	.arch_dump = NULL, 
 	.arch_import_shared = NULL, 
 	.arch_funcall = NULL, 
-	.arch_set_var = x86_64_set_var
+	.arch_set_var = x86_64_set_var, 
+	.arch_math_expr = x86_64_math_expr
 };
 
 

+ 176 - 0
arch/x86_64/x86_64_math.c

@@ -0,0 +1,176 @@
+/* tripl/arch/x86_64/x86_64_math.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/>
+ */ 
+
+
+
+
+/* parse a math-expression, with no prio
+ * i'll write down a '****' when i feel angry */
+
+#include <stdio.h>
+#include <string.h>
+
+#include <tripl/tripl.h>
+#include <tripl/arch.h>
+#include <tripl/limits.h>
+
+#include <klist.h>
+
+
+
+extern char *psave;
+extern int lno;
+
+extern struct klist_node *find_name(char *);
+
+
+__ll_asm const static char do_op_nr[] = {
+	"	%sq 	$%d, %%r12\n"
+};
+
+__ll_asm const static char do_op_glbvar[] = {
+	"	%sq 	%s(%%rip), %%r12\n"
+};
+
+__ll_asm const static char do_op_stkvar[] = {
+	"	%sq 	-%ld(%%rbp), %%r12\n"
+};
+
+__ll_asm const static char store_res_to_stkvar[] = {
+	"	movq 	%%r12, -%ld(%%rbp)\n"
+};
+
+__ll_asm const static char store_res_to_glbvar[] = {
+	"	movq 	%%r12, %s(%%rip)\n"
+};
+
+__ll_asm const static char store_r12_directly[] = {
+	"	movq 	$%ld, %%r12\n"
+};
+
+
+static int store_res(char *name) {
+	struct klist_node *n = find_name(name);
+
+	if (unlikely(!n)) 
+		exit(generic_error("name not found", psave, lno));
+
+	if (unlikely(n->xmask & 4)) 
+		exit(generic_error("variable sets to be 'read-only'", psave, lno));
+
+	if (n->xmask & 1) 
+		printf(store_res_to_glbvar, name);
+
+	else 
+		printf(store_res_to_stkvar, n->value << 3);
+
+	return 0;
+}
+
+static int operation_var(char *opr, char *name) {
+	struct klist_node *n = find_name(name);
+
+	if (unlikely(!n)) 
+		exit(generic_error("name not found", psave, lno));
+
+	if (n->xmask & 1) 
+		printf(do_op_glbvar, opr, name);
+
+	else 
+		printf(do_op_stkvar, opr, n->value << 3);
+
+	return 0;
+}
+
+
+#define operation_nr(opr, nr) 	printf(do_op_nr, opr, nr)
+
+
+ int x86_64_math_expr(char *p, char *var) {
+	int type, out;
+	int op, i;
+	static char *operations[] = {"add", "sub"};
+	char *opstr;
+
+	p++;
+	_skip_spaces(p);
+
+	/* no any prio supported */
+
+	for (i = 0; getword(p, &type, &out); i++) {
+		if (unlikely(!i)) {
+			/* the first number is the initialize value of r12 */
+
+			printf(store_r12_directly, (long) out);
+			_skip_word(p);
+			_skip_spaces(p);
+
+			continue;
+		}
+
+		/* operator */
+		op = out;
+
+		switch (op) {
+		case '+':
+			opstr = operations[0];
+			
+			break;
+
+		case '-':
+			opstr = operations[1];
+
+			break;
+
+		default:
+			exit(generic_error("invalid operator", psave, lno));
+		}
+
+		p++;
+		_skip_spaces(p);
+
+		getword(p, &type, &out);
+
+		/* number or name, add or sub */
+
+		if (type == WORDTYPE_NR) {
+			operation_nr(opstr, out);
+	
+			_skip_word(p);
+		}
+		
+		else if (type == WORDTYPE_NAME) {
+			char *name = strndup(p, out);
+
+			operation_var(opstr, name);
+
+			free(name);
+
+			p += out;
+		}
+
+		else 
+			exit(generic_error("invalid token in math expression", psave, lno));
+
+		_skip_spaces(p);
+	}
+
+	store_res(var);
+
+	return 0;
+}

+ 1 - 0
include/config.h

@@ -0,0 +1 @@
+#define CONFIG_NODISPLAY_IINI 			(0)

+ 1 - 1
include/tripl/arch.h

@@ -40,7 +40,7 @@ struct arch {
 	int (* arch_set_var) (char *name, long value, int attr);
 
 	/* var: name of variable which stores the result */
-	int (* arch_math_expr) (char *p, char *var);
+	int (* arch_math_expr) (char *p, char *vars);
 };
 
 

+ 1 - 0
main.c

@@ -51,6 +51,7 @@ int main(int argc, const char **argv) {
 
 	/* i don't like getopt()... */
 	if (!strcmp(argv[1], "-c")) {
+		fname = strdup("stdin");
 		fbuffer = (char *) argv[2];
 
 		fsize = strlen(fbuffer);

+ 4 - 39
sample.trp

@@ -1,50 +1,15 @@
-
 #! /usr/bin/tripl
 
-< "libc-2.33.so" 	@ shared;
-
-# now we're testing the c-style declares
-int main(int argc, const char **argv);
-extern int fputs(char *, int);
-
-# and tripl-style
-< add(a, b) 		@ fn;
-
-$ a;
-$ b;
-$ c;
+$ a 			@ global;
+$ b 			@ global;
+$ c 			@ global;
 
 -10 -> a;
 
 a = b = 1000;
 
-a = ` "/usr/bin/bash" {
-	echo "greeting from bash :-)"
-
-	exit 100
-};
-
-
-c = ` "./tripl" {
-	$ a = 0;
-
-	a = ` "/usr/bin/bash" {
-		echo -n 	"this is a bash and it is running in a tripl-interpreter which"
-		echo 		"are running in a tripl-interpreter"
-
-		exit 10
-	};
-
-	! 		@ var;
-};
-
 
 # @ var means, only view value of var, but not functions and libraries. 
 
-! 			@ var;
-
-b = a = $ 1 + 1 * 100 / 12;
-
-!;
-
+a = $ 1 + c;