Skip to content

Commit

Permalink
mostly fixes
Browse files Browse the repository at this point in the history
- riscv: added IR_COPY to IR_FCALL(memcpy) conversion
- riscv: disabled unfinished code
- riscv: fixed wrong include paths
- fixed test2
- fixed fix_crts.sh for cross-compilation
- fixed configure-target-libbcc
- updated TODO
- removed false comment in ir.h
  • Loading branch information
realchonk committed Aug 29, 2021
1 parent 1876135 commit d9928e4
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ full-clean: clean full-clean-target-libbcc

if ENABLE_LIBBCC
configure-target-libbcc: bcc
cd $(srcdir)/libbcc && CC=../bcc ./configure --host=$(TARGET)
cd $(srcdir)/libbcc && CC="../bcc -e ../cpp/bcpp" ./configure --host=$(TARGET)
all-target-libbcc: configure-target-libbcc
$(MAKE) -C libbcc
install-target-libbcc:
Expand Down
3 changes: 2 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ riscv:
- simplify
- fix bugs
- remove _GNU_SOURCE dependency
- make test2 be usable without installation of libbcc


# Other
- find & fix reason for random crashes
- options: -g, --sysroot=, -static, -shared, -save-temps
- <stdarg.h> header

- auto-detection of --with-cpu= & --with-abi=

# Helpful Resources/Websites
- POSIX C99 compiler - https://pubs.opengroup.org/onlinepubs/9699919799/utilities/c99.html
Expand Down
2 changes: 1 addition & 1 deletion include/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ typedef struct ir_node {
} iicast;
struct {
istr_t name;
ir_reg_t dest; // only used with IR_IFCALL
ir_reg_t dest;
struct ir_node** params;
} ifcall;
struct {
Expand Down
2 changes: 1 addition & 1 deletion src/riscv/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#include "riscv/regs.h"
#include "config.h"
#include "target.h"
#include "error.h"
#include "regs.h"

// 16 - REGSIZE
#if BITS == 32
Expand Down
3 changes: 2 additions & 1 deletion src/riscv/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include "riscv/regs.h"
#include "riscv/cpu.h"
#include "cmdline.h"
#include "target.h"
#include "error.h"
#include "regs.h"
#include "config.h"

struct machine_option mach_opts[] = {
Expand Down
4 changes: 2 additions & 2 deletions src/riscv/emit_ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#include <string.h>
#include "riscv/emit_ir.h"
#include "riscv/regs.h"
#include "riscv/cpu.h"
#include "emit_ir.h"
#include "strdb.h"
#include "error.h"
#include "regs.h"
#include "bcc.h"

static uintreg_t size_stack;
Expand Down
41 changes: 40 additions & 1 deletion src/riscv/optim.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "error.h"

// Turn IR_MUL to IR_IFCALL (if no M extension)
// TODO: implement the functions in libbcc first
static bool mul_to_func(ir_node_t** n) {
if (riscv_cpu.has_mult)
return false;
Expand Down Expand Up @@ -100,10 +101,48 @@ static bool mul_to_func(ir_node_t** n) {
return success;
}

static bool copy_to_memcpy(ir_node_t** n) {
bool success = false;
for (ir_node_t* cur = *n; cur; cur = cur->next) {
if (cur->type != IR_COPY)
continue;
ir_node_t fcall;
fcall.type = IR_FCALL;
fcall.prev = cur->prev;
fcall.next = cur->next;
fcall.func = cur->func;

fcall.ifcall.name = strint("memcpy");
fcall.ifcall.dest = cur->copy.dest;
fcall.ifcall.params = NULL;

ir_node_t* param = new_node(IR_MOVE);
param->move.dest = fcall.ifcall.dest;
param->move.src = cur->copy.dest;
param->move.size = IRS_PTR;
buf_push(fcall.ifcall.params, param);

param = new_node(IR_MOVE);
param->move.dest = fcall.ifcall.dest;
param->move.src = cur->copy.src;
param->move.size = IRS_PTR;
buf_push(fcall.ifcall.params, param);

param = new_node(IR_LOAD);
param->load.dest = fcall.ifcall.dest;
param->load.value = cur->copy.len;
param->load.value = IRS_PTR;
buf_push(fcall.ifcall.params, param);
*cur = fcall;
success = true;
}
return success;
}

// TODO: implement target-specific IR optimizations
bool target_optim_ir(ir_node_t** n) {
bool success = false;
while (mul_to_func(n))
while (copy_to_memcpy(n))
success = true;
return success;
}
1 change: 0 additions & 1 deletion src/x86/target.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ const struct target_info target_info = {
.max_immed = INT64_MAX,
.min_immed = INT64_MIN,
#endif

};

#if BITS == 32
Expand Down
2 changes: 1 addition & 1 deletion test2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ test: test.asm

else ifeq ($(ARCH),riscv)

test: test.asm
test: test.s
$(BCC) -o $@ $< $(BCCFLAGS)

test.s: test.c $(BCC)
Expand Down
12 changes: 5 additions & 7 deletions test2/test.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#include <stddef.h>

struct A {
int a;
int b;
};
int puts(const char*);

int main() {
struct A a;
return sizeof(a);
int main(void) {
char str[] = "Hello World";
puts(str);
return 0;
}
4 changes: 3 additions & 1 deletion util/fix_crts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ unset args

target="${MACHTYPE}"
GCC="${MACHTYPE}-gcc"
BCC="bcc"
force=0
fix_all=0
search_files=""
Expand Down Expand Up @@ -76,6 +77,7 @@ while true; do
;;
-t)
target="$2"
BCC="${target}-bcc"
shift 2
;;
-g)
Expand Down Expand Up @@ -103,7 +105,7 @@ fi
[ -z "$1" ] && error "operand prefix is missing"
prefix="$1"
libdir="${prefix}/${target}/lib"
bcc="${prefix}/bin/bcc"
bcc="${prefix}/bin/${BCC}"
[ ! -x "${bcc}" ] && error "couldn't to find bcc"
version="$("${bcc}" -dumpversion)" || error "failed to get bcc's version"
clibdir="${prefix}/lib/bcc/${target}/${version}"
Expand Down

0 comments on commit d9928e4

Please sign in to comment.