ls

simple syscall based programming language from scratch
git clone git://git.kocotian.pl/ls.git
Log | Files | Refs | README | LICENSE

commit 05bff73b2e5276bc003797f3eae3381c3db110f8
parent d054ac5cfe0f80656ec9f19ffc01f1d696dffd22
Author: kocotian <kocotian@kocotian.pl>
Date:   Sun, 28 Feb 2021 16:58:52 +0100

number and string literal asm code generation

Diffstat:
Mgrammar.c | 23+++++++++++++++++++++++
Mlsc.c | 5+++--
2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/grammar.c b/grammar.c @@ -2,6 +2,7 @@ and LICENSE file for license details. */ #include <stdio.h> +#include <stdlib.h> #include <string.h> #include "grammar.h" @@ -9,6 +10,7 @@ extern char *contents; extern char *output; extern size_t outsiz; +extern int sciter; static char * g_typetostr(TokenType type) @@ -33,11 +35,32 @@ size_t g_expression(Token *tokens, size_t toksize) { size_t i; + char buffer[BUFSIZ], *val; i = 0; if (tokens[i].type == TokenNumber) { /* number literal */ + val = malloc(tokens[i].len + 1); + strncpy(val, contents + tokens[i].off, tokens[i].len); + val[tokens[i].len] = '\0'; + output = realloc(output, outsiz += + snprintf(buffer, BUFSIZ, "\tmov rax, %s\n", + val)); + strncat(output, buffer, outsiz); + free(val); ++i; } else if (tokens[i].type == TokenString) { /* string literal */ + val = malloc(tokens[i].len + 1); + strncpy(val, contents + tokens[i].off, tokens[i].len); + val[tokens[i].len] = '\0'; + output = realloc(output, outsiz += + snprintf(buffer, BUFSIZ, + "section .rodata\n" + "\t.STR%d: db %s, 0\n" + "section .text\n" + "\tmov rsi, .STR%d\n", + sciter, val, sciter)); + strncat(output, buffer, outsiz); + free(val); ++i; } else if (tokens[i].type == TokenIdentifier) { /* identifier literal */ ++i; diff --git a/lsc.c b/lsc.c @@ -32,6 +32,8 @@ char *contents; char *output; size_t outsiz; +int sciter; /* string const iter */ + static int getsyscallbyname(char *name) { @@ -219,8 +221,6 @@ main(int argc, char *argv[]) int lindex; Token *tokens; - /* void *data, *bss, *text; */ - ARGBEGIN { default: usage(); @@ -230,6 +230,7 @@ main(int argc, char *argv[]) tokens = malloc(sizeof(*tokens) * (toksiz = 128)); filename = "<stdin>"; + sciter = 0; for (rb = lindex = tokiter = 0; (rb = nextline(0, buffer, BUFSIZ)) > 0; ++lindex) { contents = realloc(contents, csiz += rb);