From 045632defb10945cf72b309c79b06b4f057d0bbc Mon Sep 17 00:00:00 2001
From: herman ten brugge <hermantenbrugge@home.nl>
Date: Sat, 23 May 2020 20:27:43 +0200
Subject: [PATCH] Fix gcc10 warnings

i386-gen.c:
- load/gen_opf: set v1.sym to NULL

lib/Makefile:
- Add -gstabs -fno-omit-frame-pointer -Wno-unused-function -Wno-unused-variable

lib/bt-log.c:
- tcc_backtrace: Prevent __builtin_frame_address warning

tccgen.c:
- struct_layout: Set t.t to VT_BYTE
- default_debug: Use octal instead of -1 to make size_t work

tccpp.c:
- tal_realloc_impl: Only memcpy when p set

x86_64-gen.c:
- gen_bounds_epilog: Do not save/restore rcx (not caller/callee saved)
                     This also made stack not aligned to 16 bytes.
---
 i386-gen.c   |  2 ++
 lib/Makefile |  8 ++++++--
 lib/bt-log.c | 13 +++++++++++++
 tccgen.c     |  6 ++++--
 tccpp.c      |  6 +++---
 x86_64-gen.c |  4 ++--
 6 files changed, 30 insertions(+), 9 deletions(-)

diff --git a/i386-gen.c b/i386-gen.c
index 0c63d080..b2f2bddc 100644
--- a/i386-gen.c
+++ b/i386-gen.c
@@ -230,6 +230,7 @@ ST_FUNC void load(int r, SValue *sv)
             v1.type.t = VT_INT;
             v1.r = VT_LOCAL | VT_LVAL;
             v1.c.i = fc;
+            v1.sym = NULL;
             fr = r;
             if (!(reg_classes[fr] & RC_INT))
                 fr = get_reg(RC_INT);
@@ -923,6 +924,7 @@ ST_FUNC void gen_opf(int op)
                 v1.type.t = VT_INT;
                 v1.r = VT_LOCAL | VT_LVAL;
                 v1.c.i = fc;
+                v1.sym = NULL;
                 load(r, &v1);
                 fc = 0;
             }
diff --git a/lib/Makefile b/lib/Makefile
index f8763730..9210b38c 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -17,13 +17,17 @@ XFLAGS = $(XFLAGS$(XCFG)) -I$(TOP)
 XCFG = $(or $(findstring -win,$T),-unx)
 S = $(if $(findstring yes,$(SILENT)),@$(info * $@))
 
-# in order to use gcc, tyoe: make <target>-libtcc1-usegcc=yes
+# in order to use gcc, type: make <target>-libtcc1-usegcc=yes
 arm-libtcc1-usegcc ?= no
 
+# This makes bounds checking 40%..60% faster.
+#x86_64-libtcc1-usegcc=yes
+#i386-libtcc1-usegcc=yes
+
 ifeq "$($(T)-libtcc1-usegcc)" "yes"
  XCC = $(CC)
  XAR = $(AR)
- XFLAGS = $(CFLAGS) -fPIC
+ XFLAGS = $(CFLAGS) -fPIC -gstabs -fno-omit-frame-pointer -Wno-unused-function -Wno-unused-variable
 endif
 
 # only for native compiler
diff --git a/lib/bt-log.c b/lib/bt-log.c
index d767f08a..23a78ef3 100644
--- a/lib/bt-log.c
+++ b/lib/bt-log.c
@@ -13,6 +13,15 @@ int (*__rt_error)(void*, void*, const char *, va_list);
 # define DLL_EXPORT
 #endif
 
+#if defined(__GNUC__) && (__GNUC__ >= 6)
+/*
+ * At least gcc 6.2 complains when __builtin_frame_address is used with
+ * nonzero argument.
+ */
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wframe-address"
+#endif
+
 DLL_EXPORT int tcc_backtrace(const char *fmt, ...)
 {
     va_list ap;
@@ -35,3 +44,7 @@ DLL_EXPORT int tcc_backtrace(const char *fmt, ...)
     }
     return ret;
 }
+
+#if defined(__GNUC__) && (__GNUC__ >= 6)
+#pragma GCC diagnostic pop
+#endif
diff --git a/tccgen.c b/tccgen.c
index 5d9d871c..4aa65a70 100644
--- a/tccgen.c
+++ b/tccgen.c
@@ -140,12 +140,13 @@ static const struct {
 #if LONG_SIZE == 4
     {   VT_LONG | VT_INT | VT_UNSIGNED, "long unsigned int:t5=r5;0;4294967295;", },
 #else
-    {   VT_LLONG | VT_LONG | VT_UNSIGNED, "long unsigned int:t5=r5;0;-1;", },
+    /* use octal instead of -1 so size_t works (-gstabs+ in gcc) */
+    {   VT_LLONG | VT_LONG | VT_UNSIGNED, "long unsigned int:t5=r5;0;01777777777777777777777;", },
 #endif
     {   VT_QLONG, "__int128:t6=r6;0;-1;", },
     {   VT_QLONG | VT_UNSIGNED, "__int128 unsigned:t7=r7;0;-1;", },
     {   VT_LLONG, "long long int:t8=r8;-9223372036854775808;9223372036854775807;", },
-    {   VT_LLONG | VT_UNSIGNED, "long long unsigned int:t9=r9;0;-1;", },
+    {   VT_LLONG | VT_UNSIGNED, "long long unsigned int:t9=r9;0;01777777777777777777777;", },
     {   VT_SHORT, "short int:t10=r10;-32768;32767;", },
     {   VT_SHORT | VT_UNSIGNED, "short unsigned int:t11=r11;0;65535;", },
     {   VT_BYTE | VT_DEFSIGN, "signed char:t12=r12;-128;127;", },
@@ -4424,6 +4425,7 @@ static void struct_layout(CType *type, AttributeDef *ad)
 
         /* try to access the field using a different type */
         c0 = -1, s = align = 1;
+        t.t = VT_BYTE;
         for (;;) {
             px = f->c * 8 + bit_pos;
             cx = (px >> 3) & -align;
diff --git a/tccpp.c b/tccpp.c
index bfbb1eea..c4c1ce53 100644
--- a/tccpp.c
+++ b/tccpp.c
@@ -261,7 +261,7 @@ tail_call:
             al->p += adj_size + sizeof(tal_header_t);
             if (is_own) {
                 header = (((tal_header_t *)p) - 1);
-                memcpy(ret, p, header->size);
+                if (p) memcpy(ret, p, header->size);
 #ifdef TAL_DEBUG
                 header->line_num = -header->line_num;
 #endif
@@ -280,7 +280,7 @@ tail_call:
             al->nb_allocs--;
             ret = tal_realloc(*pal, 0, size);
             header = (((tal_header_t *)p) - 1);
-            memcpy(ret, p, header->size);
+            if (p) memcpy(ret, p, header->size);
 #ifdef TAL_DEBUG
             header->line_num = -header->line_num;
 #endif
@@ -301,7 +301,7 @@ tail_call:
         al->nb_allocs--;
         ret = tcc_malloc(size);
         header = (((tal_header_t *)p) - 1);
-        memcpy(ret, p, header->size);
+        if (p) memcpy(ret, p, header->size);
 #ifdef TAL_DEBUG
         header->line_num = -header->line_num;
 #endif
diff --git a/x86_64-gen.c b/x86_64-gen.c
index cc5c95a0..df7ae1c6 100644
--- a/x86_64-gen.c
+++ b/x86_64-gen.c
@@ -758,12 +758,12 @@ static void gen_bounds_epilog(void)
     ind = saved_ind;
 
     /* generate bound check local freeing */
-    o(0x525051); /* save returned value, if any (+ scratch-space for windows) */
+    o(0x5250); /* save returned value, if any */
     greloca(cur_text_section, sym_data, ind + 2, R_X86_64_64, 0);
     o(0xb848 + TREG_FASTCALL_1 * 0x100); /* mov xxx, %rcx/di */
     gen_le64 (0);
     gen_bounds_call(TOK___bound_local_delete);
-    o(0x59585a); /* restore returned value, if any */
+    o(0x585a); /* restore returned value, if any */
 }
 #endif