...
 
Commits (314)
/Makefile.conf
/makesys.cache
# TGT/HOST
*.exe
*.so
*.dll
*.dylib
*.a
*.c.o
*.cpp.o
*.s.o
*.S.o
*.rc.o
*.rc.res
# TEST
*.ok
*.notok
/aux/libaux.a
/compiler/bmc
image: registry.git.bitmycode.com:443/bmc/buildingtoolscontainer/master:c837e167a18ba02696ea2e641b78cc92fb89080d
.build-targets-common: &build-targets-common
stage: build-targets
variables:
MAKE_TGT: ''
script:
- eval make defconfig $MAKE_TGT
- eval make $MAKE_TGT
stages:
- build
- test
- build-targets
build:
<<: *build-targets-common
stage: build
cache:
key: build-cache
policy: push
untracked: true
variables:
MAKE_TGT: 'all'
# run tests using the binary built before
test:
<<: *build-targets-common
stage: test
cache:
key: build-cache
policy: pull
untracked: true
variables:
MAKE_TGT: 'test'
.build-linux-targets-common: &build-linux-targets-common
<<: *build-targets-common
artifacts:
paths:
- compiler/bmc
build-linux-i686:
<<: *build-linux-targets-common
variables:
MAKE_TGT: 'all OS=linux CC=x86_64-linux-gnu-gcc CFLAGS=-m32 LDFLAGS=-m32'
build-linux-x86_64:
<<: *build-linux-targets-common
variables:
MAKE_TGT: 'all OS=linux CC=x86_64-linux-gnu-gcc'
.build-windows-targets-common: &build-windows-targets-common
<<: *build-targets-common
artifacts:
paths:
- compiler/bmc.exe
build-windows-i686:
<<: *build-windows-targets-common
variables:
MAKE_TGT: 'OS=windows CC=i686-w64-mingw32-gcc'
build-windows-x86_64:
<<: *build-windows-targets-common
variables:
MAKE_TGT: 'OS=windows CC=x86_64-w64-mingw32-gcc'
\ No newline at end of file
Guillaume Charifi <guillaume.charifi@bitmycode.com>
Solène Fernandez Serrano <solene.fernandez-serrano@tuta.io>
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
# Enable delayed variable expansion
.SECONDEXPANSION:
# Disable any existing implicit rule
.SUFFIXES:
all: build
TOP := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
THIS := $(TOP)/Makefile
MKCONFIG := $(TOP)/Makefile.conf
TOP_BUILDDIR ?= $(CURDIR)
CACHEDIR := $(TOP)/makesys.cache
CUR_SUBDIR ?= /
LOCALCACHEDIR := $(CACHEDIR)$(CUR_SUBDIR)
empty :=
V ?=
V_ECHO_CMD := @
V_SILENT_CMD := @
ifneq ("x$(V)","x")
V_ECHO_CMD := @true $(empty)
V_SILENT_CMD :=
endif
ifneq ("x$(wildcard $(MKCONFIG))","x")
include $(MKCONFIG)
else ifneq ("x$(word 1, $(MAKECMDGOALS))","xdefconfig")
$(error Please run make defconfig)
endif
MAKEEXT := $(MAKE) TOP_BUILDDIR=$(TOP_BUILDDIR)
MAKETHIS := $(MAKE) -f $(THIS) TOP_BUILDDIR=$(TOP_BUILDDIR)
CCDEPS := $(CC) -MD -MP
HOSTCCDEPS := $(HOSTCC) -MD -MP
CPPDEPS := $(CPP) -MD -MP
HOSTCPPDEPS := $(HOSTCPP) -MD -MP
CXXDEPS := $(CXX) -MD -MP
HOSTCXXDEPS := $(HOSTCXX) -MD -MP
include Makefile.mk
BUILDEXTDIRS := $(EXTDIRS-y:%=build-%)
TESTEXTDIRS := $(EXTDIRS-y:%=test-%)
CLEANEXTDIRS := $(EXTDIRS-y:%=clean-%)
.PHONY: $(BUILDEXTDIRS)
.PHONY: $(TESTEXTDIRS)
.PHONY: $(CLEANEXTDIRS)
BUILDSUBDIRS := $(SUBDIRS-y:%=build-%)
TESTSUBDIRS := $(SUBDIRS-y:%=test-%)
CLEANSUBDIRS := $(SUBDIRS-y:%=clean-%)
.PHONY: $(BUILDSUBDIRS)
.PHONY: $(TESTSUBDIRS)
.PHONY: $(CLEANSUBDIRS)
OPTBUILDSUBDIRS := $(OPTSUBDIRS-y:%=build-%)
OPTTESTSUBDIRS := $(OPTSUBDIRS-y:%=test-%)
OPTCLEANSUBDIRS := $(OPTSUBDIRS-y:%=clean-%)
.PHONY: $(OPTBUILDSUBDIRS)
.PHONY: $(OPTTESTSUBDIRS)
.PHONY: $(OPTCLEANSUBDIRS)
.PHONY: build-all-TGT clean-all-TGT
.PHONY: build-all-HOST clean-all-HOST
.PHONY: build-all-CUST clean-all-CUST
.PHONY: test-all-TEST clean-all-TEST
.PHONY: all build build-all
.PHONY: test test-all
.PHONY: clean clean-all
defconfig:
$(V_ECHO_CMD)echo " GEN Makefile.conf"
$(V_SILENT_CMD)cp $(MKCONFIG).sample $(MKCONFIG)
$(LOCALCACHEDIR):
$(V_ECHO_CMD)echo " MKDIR $(LOCALCACHEDIR)"
$(V_SILENT_CMD)mkdir -p $(LOCALCACHEDIR)
build: $(LOCALCACHEDIR) $(BUILDEXTDIRS) $(BUILDSUBDIRS)
+@$(MAKETHIS) --no-print-directory CUR_SUBDIR=$(CUR_SUBDIR) build-all
$(BUILDEXTDIRS)::
+@$(MAKEEXT) -C $(@:build-%=%) build
$(BUILDSUBDIRS):: $(BUILDEXTDIRS)
+@$(MAKETHIS) -C $(@:build-%=%) CUR_SUBDIR=$(CUR_SUBDIR)$(@:build-%=%)/ build
build-all: build-all-TGT build-all-HOST build-all-CUST
$(OPTBUILDSUBDIRS):
+@$(MAKETHIS) -C $(@:build-%=%) CUR_SUBDIR=$(CUR_SUBDIR)$(@:build-%=%)/ build
test: build $(LOCALCACHEDIR) $(TESTEXTDIRS) $(TESTSUBDIRS)
+@$(MAKETHIS) --no-print-directory CUR_SUBDIR=$(CUR_SUBDIR) test-all
$(TESTEXTDIRS)::
+@$(MAKEEXT) -C $(@:test-%=%) test
$(TESTSUBDIRS):: $(TESTEXTDIRS)
+@$(MAKETHIS) -C $(@:test-%=%) CUR_SUBDIR=$(CUR_SUBDIR)$(@:test-%=%)/ test
test-all: test-all-TEST
$(OPTTESTSUBDIRS):
+@$(MAKETHIS) -C $(@:test-%=%) CUR_SUBDIR=$(CUR_SUBDIR)$(@:test-%=%)/ test
clean: $(CLEANEXTDIRS) $(CLEANSUBDIRS)
+@$(MAKETHIS) --no-print-directory CUR_SUBDIR=$(CUR_SUBDIR) clean-all
$(CLEANEXTDIRS)::
+@$(MAKEEXT) -C $(@:clean-%=%) clean
$(CLEANSUBDIRS):: $(CLEANEXTDIRS)
+@$(MAKETHIS) -C $(@:clean-%=%) CUR_SUBDIR=$(CUR_SUBDIR)$(@:clean-%=%)/ clean
clean-all: clean-all-TGT clean-all-HOST clean-all-CUST clean-all-TEST
$(OPTCLEANSUBDIRS):
+@$(MAKETHIS) -C $(@:clean-%=%) CUR_SUBDIR=$(CUR_SUBDIR)$(@:clean-%=%)/ clean
# Make all intermediate files persistent.
.SECONDARY:
ALL_TARGETS :=
include $(TOP)/mk/Makefile.tgt
include $(TOP)/mk/Makefile.host
include $(TOP)/mk/Makefile.cust
include $(TOP)/mk/Makefile.test
$(ALL_TARGETS): $(MKCONFIG) $(THIS) $(TOP)/mk/Makefile.tgt $(TOP)/mk/Makefile.host $(TOP)/mk/Makefile.cust $(TOP)/mk/Makefile.test $(CURDIR)/Makefile.mk
# Copy to Makefile.conf to make it work
##### Configure values here #####
MAKE := make
CC ?= gcc
CPP ?= cpp
CCLD ?= $(CC)
CXX ?= g++
CXXLD ?= $(CXX)
AR ?= ar
RANLIB ?= ranlib
WINDRES ?= windres
OBJCOPY ?= objcopy
HOSTCC ?= gcc
HOSTCPP ?= cpp
HOSTCCLD ?= $(HOSTCC)
HOSTCXX ?= g++
HOSTCXXLD ?= $(HOSTCXX)
HOSTAR ?= ar
HOSTRANLIB ?= ranlib
HOSTWINDRES ?= windres
HOSTOBJCOPY ?= objcopy
CFLAGS ?=
CFLAGS_DBG ?= -g
CFLAGS_OPT ?= -O0
CFLAGS-y := -std=c11 -Wall -Wextra -pedantic $(CFLAGS) $(CFLAGS_DBG) $(CFLAGS_OPT)
LDFLAGS ?=
LDFLAGS-y := $(LDFLAGS)
CXXFLAGS ?= $(CFLAGS)
CXXFLAGS_DBG ?= $(CFLAGS_DBG)
CXXFLAGS_OPT ?= $(CFLAGS_OPT)
CXXFLAGS-y := -std=c++11 -Wall -Wextra -pedantic $(CXXFLAGS) $(CXXFLAGS_DBG) $(CXXFLAGS_OPT)
OS ?= linux
##### Do NOT edit anything after this comment #####
CFLAGS-y += -I$(TOP) -I$(TOP)/include
LDFLAGS_SHARED := -shared
STATIC_LIB_EXT := .a
ifeq ("x$(OS)", "xlinux")
CONFIG_OS_LINUX := y
DYN_LIB_EXT := .so
EXE_EXT :=
CFLAGS-y += -DCONFIG_OS_LINUX
else ifeq ("x$(OS)", "xwindows")
CONFIG_OS_WINDOWS := y
DYN_LIB_EXT := .dll
EXE_EXT := .exe
CFLAGS-y += -DCONFIG_OS_WINDOWS
LDFLAGS-y += -static-libgcc
else ifeq ("x$(OS)", "xmacosx")
CONFIG_OS_MACOSX := y
DYN_LIB_EXT := .dylib
EXE_EXT :=
CFLAGS-y += -DCONFIG_OS_MACOSX
else
$(error Unknown or undefined $$(OS): "$(OS)")
endif
SUBDIRS-y := aux compiler
BootMonkey
==========
/*
* Copyright (C) 2014-2016 Guillaume Charifi
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include <memory.h>
#include <ctype.h>
#include <stdint.h>
#include <string.h>
#undef HAVE_LINUX_FILES
#if (defined HAVE_SYS_TYPES_H) \
&& (defined HAVE_SYS_STAT_H) \
&& (defined HAVE_FCNTL_H) \
&& (defined HAVE_UNISTD_H) \
&& (defined HAVE_MMAP) \
&& (defined HAVE_MUNMAP) \
&& (defined HAVE_FDOPEN)
# define HAVE_LINUX_FILES 1
#endif
#if HAVE_LINUX_FILES
# include <sys/types.h>
# include <sys/stat.h>
# include <sys/mman.h>
# include <fcntl.h>
# include <unistd.h>
#endif
#include "common/utils.h"
#include "error.h"
#include "blocks.h"
#include "bytecodes.h"
static int64_t get_file_size(FILE *fp)
{
int64_t old, size;
old = ftell(fp);
fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, old, SEEK_SET);
return size;
}
static void disp_usage(const char *argv0)
{
fatal("Usage: %s [in.bmas] [-o out.bmo]\n", argv0);
}
int main(int argc, char **argv)
{
int retval = 1;
int ret = 0;
#ifdef HAVE_LINUX_FILES
int fdin = -1;
int is_mmaped = 0;
#endif
FILE *in = stdin;
FILE *out = stdout;
int64_t infile_len = 0;
char *in_name = "(stdin)";
char *inbuf = NULL;
uint64_t outbuf_len = 0;
char *out_name = "(stdout)";
uint8_t *outbuf = NULL;
switch(argc) {
case 1:
break;
case 2:
case 3:
case 4: {
int i = 1;
while(i < argc) {
if(out && strcmp(argv[i], "-o") == 0) {
if(i + 1 >= argc) {
disp_usage(argv[0]);
goto free_exit;
}
out_name = argv[i + 1];
out = NULL;
i += 2;
} else if(in) {
in_name = argv[i];
in = NULL;
i++;
} else {
disp_usage(argv[0]);
goto free_exit;
}
}
break;
}
default:
disp_usage(argv[0]);
goto free_exit;
}
#ifdef HAVE_LINUX_FILES
if(!in) {
fdin = open(in_name, O_RDONLY);
if(fdin < 0) {
fatal("Unable to read from \"%s\".\n", in_name);
goto free_exit;
}
in = fdopen(fdin, "rb");
if(!in) {
fatal("Unable to retrieve a stream for \"%s\".\n", in_name);
goto free_exit;
}
}
#else
if(!in) {
in = fopen(in_name, "rb");
if(!in) {
fatal("Unable to read from \"%s\".\n", in_name);
goto free_exit;
}
}
#endif
if(!out) {
out = fopen(out_name, "wb");
if(!out) {
fatal("Unable to write to \"%s\".\n", out_name);
goto free_exit;
}
}
infile_len = get_file_size(in);
/* Empty file, nothing more to do. */
if(infile_len == 0) {
retval = 0;
goto free_exit;
}
/* The file size could not be determined (e.g. streamed input). */
if(infile_len < 0) {
char *new_inbuf = NULL;
infile_len = 0;
while(!feof(in)) {
uint64_t read_len = 0;
new_inbuf = realloc(inbuf, infile_len + 4096);
if(!new_inbuf) {
fatal("Unable to allocate memory for input file \"%s\".\n", in_name);
goto free_exit;
}
inbuf = new_inbuf;
read_len = fread(&inbuf[infile_len], 1, 4096, in);
if(ferror(in)) {
fatal("Unable to read from input file \"%s\".\n", in_name);
goto free_exit;
}
infile_len += read_len;
}
/* Empty file, nothing more to do. */
if(infile_len == 0) {
retval = 0;
goto free_exit;
}
}
#ifdef HAVE_LINUX_FILES
/* If the file size could be determined, first try to map it. */
if(!inbuf) {
is_mmaped = 1;
inbuf = mmap(NULL, infile_len, PROT_READ, MAP_SHARED, fdin, 0);
if(inbuf == MAP_FAILED) {
is_mmaped = 0;
inbuf = NULL;
}
}
#endif
/* Or fallback to standard dump to memory. */
if(!inbuf) {
inbuf = malloc(infile_len);
if(!inbuf) {
fatal("Unable to allocate memory for input file \"%s\".\n", in_name);
goto free_exit;
}
if(fread(inbuf, 1, infile_len, in) < infile_len && ferror(in)) {
fatal("Unable to read from input file \"%s\".\n", in_name);
goto free_exit;
}
}
char *inbuf_off = inbuf;
char *next = NULL;
unsigned int newline_count = 0;
while(inbuf_off < &inbuf[infile_len]) {
const uint64_t rem_len = (uint64_t)(&inbuf[infile_len] - inbuf_off);
newline_count++;
ret = create_bytecode_from_str(inbuf_off, &next, rem_len);
if(ret < 0) {
char *msg_instr_end;
uint64_t length;
char *msg;
char *msg_instr;
msg_instr_end = memchr(inbuf_off, '\n', rem_len);
if(!msg_instr_end)
msg_instr_end = &inbuf[infile_len];
length = rem_len;
switch(ret) {
case ERR_INTERNAL:
msg = "FATAL: Internal error while processing instruction at %s:%u:\n%s\n";
break;
case ERR_INVAL_INSTR:
msg = "FATAL: Invalid instruction at %s:%u:\n%s\n";
break;
case ERR_INVAL_PARAM:
msg = "FATAL: Invalid parameter at %s:%u:\n%s\n";
break;
case ERR_INVAL_REG:
msg = "FATAL: Invalid register at %s:%u:\n%s\n";
break;
case ERR_MISSING_PARAM:
msg = "FATAL: Missing parameter at %s:%u:\n%s\n";
break;
case ERR_DUPL_LABEL:
msg = "FATAL: Duplicated label at %s:%u:\n%s\n";
break;
case ERR:
default:
msg = "FATAL: Syntax error at %s:%u:\n%s\n";
break;
}
msg_instr = malloc(length + 1);
if(!msg_instr) {
fatal("Unable to allocate memory for the error message of the instruction at %s:%u.\n", in_name, newline_count);
goto free_exit;
}
memcpy(msg_instr, inbuf_off, length);
msg_instr[length] = '\0';
fprintf(stderr, msg, in_name, newline_count, msg_instr);
free(msg_instr);
goto free_exit;
}
inbuf_off = next;
}
ret = block_bind(&outbuf, &outbuf_len);
if(ret < 0) {
char *msg;
switch(ret) {
case ERR:
default:
msg = "FATAL: Unable to bind label.\n";
break;
}
fprintf(stderr, "%s", msg);
goto free_exit;
}
fwrite(outbuf, 1, outbuf_len, out);
retval = 0;
free_exit:
block_release();
#ifdef HAVE_LINUX_FILES
if(inbuf && is_mmaped) {
munmap(inbuf, infile_len);
inbuf = NULL;
}
#endif
if(inbuf)
free(inbuf);
if(in && in != stdin)
fclose(in);
if(out && out != stdout) {
fclose(out);
if(retval != 0)
remove(out_name);
}
exit(retval);
}
/*
* Copyright (C) 2014-2016 Guillaume Charifi
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <ctype.h>
#include <inttypes.h>
#include <stdint.h>
#include <errno.h>
#include "common/encoding.h"
#include "common/utils.h"
#include "blocks.h"
#include "error.h"
#include "bytecodes.h"
struct s_bm_label
{
char *label;
uint64_t off;
int is_resolved;
};
struct s_bm_weakref
{
char *label;
uint64_t instr_off;
uint64_t off;
uint64_t len;
int force_param_len;
int is_resolved;
int is_fixedup;
struct s_bm_label *bm_label;
};
#define DATA_SIZE_INCR 1024
static uint64_t nlabels = 0;
static struct s_bm_label *labels = NULL;
static uint64_t nweakrefs = 0;
static struct s_bm_weakref *weakrefs = NULL;
static uint64_t bin_data_len = 0;
static uint64_t bin_data_real_len = 0;
static uint8_t *bin_data = NULL;
uint64_t block_get_off(void)
{
return bin_data_len;
}
int block_add_label(char *label, uint64_t label_len)
{
struct s_bm_label *new_labels = NULL;
char *new_label = NULL;
if(!label_len)
return ERR_INTERNAL;
/* Do not allow duplicated labels. */
for(uint64_t i = 0; i < nlabels; i++)
if(strncmp(labels[i].label, label, label_len) == 0)
return ERR_DUPL_LABEL;
new_label = malloc(label_len + 1);
if(!new_label)
return ERR_INTERNAL;
strncpy(new_label, label, label_len);
new_label[label_len] = '\0';
new_labels = realloc(labels, sizeof(*labels) * (nlabels + 1));
if(!new_labels) {
free(new_label);
return ERR_INTERNAL;
}
labels = new_labels;
labels[nlabels].label = new_label;
labels[nlabels].off = bin_data_len;
labels[nlabels].is_resolved = 0;
nlabels++;
return SUCCESS;
}
int block_add_data(uint8_t *data, uint64_t len) {
uint8_t *new_bin_data = NULL;
if(!len)
return SUCCESS;
if(bin_data_len + len > bin_data_real_len) {
new_bin_data = realloc(bin_data, bin_data_real_len + DATA_SIZE_INCR);
if(!new_bin_data)
return ERR_INTERNAL;
bin_data_real_len += DATA_SIZE_INCR;
bin_data = new_bin_data;
}
memcpy(&bin_data[bin_data_len], data, len);
bin_data_len += len;
return SUCCESS;
}
int block_add_weakref(char *label, uint64_t label_len, uint64_t instr_off, uint64_t off, uint64_t len, int force_param_len)
{
struct s_bm_weakref *new_weakrefs = NULL;
char *new_label = NULL;
if(!len)
return SUCCESS;
if(!label_len)
return ERR_INTERNAL;
new_label = malloc(label_len + 1);
if(!new_label)
return ERR_INTERNAL;
strncpy(new_label, label, label_len);
new_label[label_len] = '\0';
new_weakrefs = realloc(weakrefs, sizeof(*weakrefs) * (nweakrefs + 1));
if(!new_weakrefs) {
free(new_label);
return ERR_INTERNAL;
}
weakrefs = new_weakrefs;
weakrefs[nweakrefs].label = new_label;
weakrefs[nweakrefs].instr_off = instr_off;
weakrefs[nweakrefs].off = off;
weakrefs[nweakrefs].len = len;
weakrefs[nweakrefs].force_param_len = force_param_len;
weakrefs[nweakrefs].is_resolved = 0;
weakrefs[nweakrefs].is_fixedup = 0;
nweakrefs++;
return SUCCESS;
}
int block_bind(uint8_t **dest, uint64_t *len)
{
/* Bind each label to its references. */
for(uint64_t i = 0; i < nlabels; i++) {
if(labels[i].is_resolved)
continue;
for(uint64_t j = 0; j < nweakrefs; j++) {
uint8_t attrs = 0;
uint64_t reldiff = 0;
uint64_t off = 0;
if(weakrefs[j].is_resolved)
continue;
if(strcmp(labels[i].label, weakrefs[j].label) != 0)
continue;
attrs = read_u8(&bin_data[weakrefs[j].off]);
reldiff = abs_u64(labels[i].off - weakrefs[j].instr_off);
if(!weakrefs[j].force_param_len) {
uint64_t attr_len = 0;
if(reldiff > labels[i].off) {
attrs |= PARAM_PTR_ABS;
attr_len = labels[i].off;
} else {
attrs |= PARAM_PTR_REL;
attr_len = reldiff;
}
if(attr_len <= 0xFF)
attrs |= PARAM_BIT8;
else if(attr_len <= 0xFFFF)
attrs |= PARAM_BIT16;
else if(attr_len <= 0xFFFFFFFF)
attrs |= PARAM_BIT32;
else if(attr_len <= 0xFFFFFFFFFFFFFFFF)
attrs |= PARAM_BIT64;
}
if((attrs & PARAM_PTRTYPEMASK) == PARAM_PTR_ABS)
off = labels[i].off;
else
off = labels[i].off - weakrefs[j].instr_off;
write_u8(&bin_data[weakrefs[j].off], attrs);
switch(attrs & PARAM_LENMASK) {
case PARAM_BIT8:
weakrefs[j].len = sizeof(uint8_t);
write_u8(&bin_data[weakrefs[j].off + 1], (uint8_t)off);
break;
case PARAM_BIT16:
weakrefs[j].len = sizeof(uint16_t);
write_u16(&bin_data[weakrefs[j].off + 1], (uint16_t)off);
break;
case PARAM_BIT32:
weakrefs[j].len = sizeof(uint32_t);
write_u32(&bin_data[weakrefs[j].off + 1], (uint32_t)off);
break;
case PARAM_BIT64:
weakrefs[j].len = sizeof(uint64_t);
write_u64(&bin_data[weakrefs[j].off + 1], (uint64_t)off);
break;
}
free(weakrefs[j].label);
weakrefs[j].label = NULL;
weakrefs[j].is_resolved = 1;
weakrefs[j].bm_label = &labels[i];
}
free(labels[i].label);
labels[i].label = NULL;
labels[i].is_resolved = 1;
}
/* Check that all references are resolved. */
for(uint64_t i = 0; i < nweakrefs; i++)
if(!weakrefs[i].is_resolved)
return ERR_UNRESOLVED_REF;
/* Adjust offsets. */
for(uint64_t i = 0; i < nweakrefs; i++) {
if(weakrefs[i].is_fixedup)
continue;
weakrefs[i].is_fixedup = 1;
if(weakrefs[i].len == sizeof(uint64_t))
continue;
memmove(&bin_data[weakrefs[i].off + 1 + weakrefs[i].len], &bin_data[weakrefs[i].off + 1 + sizeof(uint64_t)], bin_data_len - (weakrefs[i].off + 1 + sizeof(uint64_t)));
bin_data_len -= sizeof(uint64_t) - weakrefs[i].len;
for(uint64_t j = 0; j < nlabels; j++) {
if(labels[j].off < weakrefs[i].off + weakrefs[i].len)
continue;
labels[j].off -= sizeof(uint64_t) - weakrefs[i].len;
}
for(uint64_t j = 0; j < nweakrefs; j++) {
uint8_t attrs = 0;
uint64_t off = 0;
if(weakrefs[j].off >= weakrefs[i].off + weakrefs[i].len)
weakrefs[j].off -= sizeof(uint64_t) - weakrefs[i].len;
if(weakrefs[j].instr_off >= weakrefs[i].off + weakrefs[i].len)
weakrefs[j].instr_off -= sizeof(uint64_t) - weakrefs[i].len;
attrs = bin_data[weakrefs[j].off];
if((attrs & PARAM_PTRTYPEMASK) == PARAM_PTR_ABS)
off = (uint64_t)weakrefs[j].bm_label->off;
else
off = (uint64_t)(weakrefs[j].bm_label->off - weakrefs[j].instr_off);
switch(attrs & PARAM_LENMASK) {
case PARAM_BIT8:
write_u8(&bin_data[weakrefs[j].off + 1], (uint8_t)off);
break;
case PARAM_BIT16:
write_u16(&bin_data[weakrefs[j].off + 1], (uint16_t)off);
break;
case PARAM_BIT32:
write_u32(&bin_data[weakrefs[j].off + 1], (uint32_t)off);
break;
case PARAM_BIT64:
write_u64(&bin_data[weakrefs[j].off + 1], (uint64_t)off);
break;
}
}
}
*dest = bin_data;
*len = bin_data_len;
return SUCCESS;
}
void block_release(void)
{
for(uint64_t i = 0; i < nlabels; i++)
if(labels[i].label)
free(labels[i].label);
nlabels = 0;
if(labels)
free(labels);
labels = NULL;
for(uint64_t i = 0; i < nweakrefs; i++)
if(weakrefs[i].label)
free(weakrefs[i].label);
nweakrefs = 0;
if(weakrefs)
free(weakrefs);
weakrefs = NULL;
bin_data_len = 0;
if(bin_data)
free(bin_data);
bin_data = NULL;
}
/*
* Copyright (C) 2014-2015 Guillaume Charifi
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef BLOCKS_H
#define BLOCKS_H
#include <stdint.h>
extern uint64_t block_get_off(void);
extern int block_add_label(char *label, uint64_t label_len);
extern int block_add_data(uint8_t *data, uint64_t len);
extern int block_add_weakref(char *label, uint64_t label_len, uint64_t instr_off, uint64_t off, uint64_t len, int force_param_len);
extern int block_bind(uint8_t **dest, uint64_t *len);
extern void block_release(void);
#endif
This diff is collapsed.
/*
* Copyright (C) 2014-2015 Guillaume Charifi
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef BYTECODES_H
#define BYTECODES_H
#define PARAM_TYPEMASK (1 & 1)
#define PARAM_REG (0 & 1)
#define PARAM_IMM (1 & 1)
#define PARAM_LENMASK ((3 & 3) << 1)
#define PARAM_BIT8 ((0 & 3) << 1)
#define PARAM_BIT16 ((1 & 3) << 1)
#define PARAM_BIT32 ((2 & 3) << 1)
#define PARAM_BIT64 ((3 & 3) << 1)
#define PARAM_ACCESSMASK ((1 & 1) << 3)
#define PARAM_VAL ((0 & 1) << 3)
#define PARAM_PTR ((1 & 1) << 3)
#define PARAM_PTRTYPEMASK ((1 & 1) << 4)
#define PARAM_PTR_ABS ((0 & 1) << 4)
#define PARAM_PTR_REL ((1 & 1) << 4)
#define PARAM_DSTLENMASK ((3 & 3) << 5)
#define PARAM_PTR_DST8 ((0 & 3) << 5)
#define PARAM_PTR_DST16 ((1 & 3) << 5)
#define PARAM_PTR_DST32 ((2 & 3) << 5)
#define PARAM_PTR_DST64 ((3 & 3) << 5)
extern int create_bytecode_from_str(char *str, char **next, uint64_t str_max_len);
#endif
/*
* Copyright (C) 2014-2015 Guillaume Charifi
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef ERROR_H
#define ERROR_H
#define SUCCESS 0
#define ERR -1
#define ERR_INTERNAL -2
#define ERR_INVAL_INSTR -3
#define ERR_INVAL_REG -4
#define ERR_INVAL_PARAM -5
#define ERR_MISSING_PARAM -6
#define ERR_DUPL_LABEL -7
#define ERR_UNRESOLVED_REF -8
#endif
SUBDIRS-y :=
EXTDIRS-y :=
libaux := libaux$(STATIC_LIB_EXT)
STATICLIBS-y := $(libaux)
objs := \
file.c.o
CFLAGS-y += -I../include -I../include/internal -I../include/internal/aux
OBJS-$(libaux)-y := $(objs)
/*
* Copyright (C) 2017 Guillaume Charifi
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "file.h"
#define BLK_SIZE 4096
static int std_mode_from_aux_mode(char std_mode[4], enum aux_file_open_mode mode)
{
if (!(mode & AUX_FILE_OPEN_READ)
&& !(mode & AUX_FILE_OPEN_WRITE))
return -1;
if (mode & AUX_FILE_OPEN_READ)
*std_mode++ = 'r';
if (mode & AUX_FILE_OPEN_WRITE)
*std_mode++ = 'w';
if (mode & AUX_FILE_OPEN_BIN)
*std_mode++ = 'b';
*std_mode++ = '\0';
return 0;
}
int aux_file_open(struct aux_file *file, const char *path, enum aux_file_open_mode mode)
{
int status;
char std_mode[4];
FILE *fp;
file->fp = NULL;
file->mode = 0;
file->size = 0;
status = std_mode_from_aux_mode(std_mode, mode);
if (status < 0)
return status;
fp = fopen(path, std_mode);
if (!fp)
return -1;
file->fp = fp;
file->mode = mode;
return 0;
}
int64_t aux_file_get_size(struct aux_file *file)
{
FILE *fp = file->fp;
long old, size;
old = ftell(fp);
fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, old, SEEK_SET);
if (size < 0)
return file->size;
return size;
}
void *aux_file_get_contents(struct aux_file *file, uint64_t *read_len)
{
static const char null_mb[] = u8"";
size_t null_mb_size = sizeof(null_mb);
int status;
FILE *fp = file->fp;
int64_t size;
char *data = NULL;
size = aux_file_get_size(file);
if (size > 0)
{
data = malloc(size + null_mb_size);
if (!data)
return NULL;
status = fread(data, size, 1, fp);
if (status < 1)
goto free_exit;
memcpy(&data[size], null_mb, null_mb_size);
}
if (size < 0)
{
size = 0;
while (!feof(fp))
{
char *new_data;
uint64_t read_size = 0;
new_data = realloc(data, size + BLK_SIZE + null_mb_size);
if (!new_data)
goto free_exit;
data = new_data;
read_size = fread(&data[size], 1, BLK_SIZE, fp);
if (ferror(fp))
goto free_exit;
size += read_size;
}
/* Empty file, nothing more to do. */
if (size == 0)
goto free_exit;
memcpy(&data[size], null_mb, null_mb_size);
}
if (read_len)
*read_len = size;
return data;
free_exit:
if (data)
free(data);
return NULL;
}
int aux_file_close(struct aux_file *file)
{
if (file->fp)
fclose(file->fp);
file->fp = NULL;
return 0;
}
SUBDIRS-y := tests
EXTDIRS-y :=
bmc := bmc$(EXE_EXT)
BINS-y := $(bmc)
objs := \
compiler.c.o \
lexer.c.o \
parse_expr.c.o \
parse_instr.c.o \
parser.c.o \
utils.c.o
CFLAGS-y += -I../include -I../include/internal
LDFLAGS-y += -L../aux -laux
OBJS-$(bmc)-y := $(objs)
/*
* Copyright (C) 2017 Guillaume Charifi
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef AST_H
#define AST_H
#include <wchar.h>
#include "ast_node_type.h"
#include "utils.h"
enum bm_ast_annotation_type
{
BM_AST_ANNOTATION_EXPLICIT_ORDER = 0,
BM_AST_ANNOTATION_MAIN,
BM_AST_ANNOTATION_READONLY
};
enum bm_ast_annotation_mask
{
BM_AST_ANNOTATION_EXPLICIT_ORDER_BIT = 1u << BM_AST_ANNOTATION_EXPLICIT_ORDER,
BM_AST_ANNOTATION_MAIN_BIT = 1u << BM_AST_ANNOTATION_MAIN,
BM_AST_ANNOTATION_READONLY_BIT = 1u << BM_AST_ANNOTATION_READONLY
};
enum bm_ast_visibility
{
BM_AST_VISIBILITY_INVAL = 0,
BM_AST_VISIBILITY_PUBLIC,
BM_AST_VISIBILITY_PRIVATE,
BM_AST_VISIBILITY_PROTECTED
};
enum bm_ast_val_imm_type
{
BM_AST_VALUE_IMM_BOOL = 0,
BM_AST_VALUE_IMM_CHAR,
BM_AST_VALUE_IMM_FLOAT,
BM_AST_VALUE_IMM_INT,
BM_AST_VALUE_IMM_STRING,
BM_AST_VALUE_IMM_UINT
};
enum bm_ast_bool
{
BM_AST_BOOL_FALSE = 0,
BM_AST_BOOL_TRUE
};
struct bm_ast_class_path
{
struct buffer seqs;
};
struct bm_ast_type
{
struct bm_ast_class_path class_path;
wchar_t *class_name;
unsigned long nb_generic_params;
struct bm_ast_type *generic_params;
};
struct bm_ast_decl
{
struct bm_ast_type type;
wchar_t *name;
};
struct bm_ast_decl_func
{
struct bm_ast_decl base;