|
@@ -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;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|