aboutsummaryrefslogtreecommitdiff
path: root/tcc.h
diff options
context:
space:
mode:
authorEdmund Grimley Evans <Edmund.Grimley.Evans@gmail.com>2015-11-19 18:21:14 +0000
committerEdmund Grimley Evans <Edmund.Grimley.Evans@gmail.com>2015-11-19 18:21:14 +0000
commit553242c18a2387fb896df66eb9b5a502f0e87ff0 (patch)
tree51501263aac1ea67fffb969466c31004cec9060e /tcc.h
parent5d496b169580d0cd4a1fbb454fc1ac55a78a0ee5 (diff)
downloadtinycc-553242c18a2387fb896df66eb9b5a502f0e87ff0.tar.gz
tinycc-553242c18a2387fb896df66eb9b5a502f0e87ff0.tar.bz2
Replace pointer casts with calls to (read|write)(16|32|64)le.
This stops UBSan from giving runtime misaligned address errors and might eventually allow building on a non-little-endian host.
Diffstat (limited to 'tcc.h')
-rw-r--r--tcc.h45
1 files changed, 45 insertions, 0 deletions
diff --git a/tcc.h b/tcc.h
index ff846fd..1be6be7 100644
--- a/tcc.h
+++ b/tcc.h
@@ -131,6 +131,51 @@
#include "stab.h"
#include "libtcc.h"
+static inline uint16_t read16le(unsigned char *p)
+{
+ return p[0] | (uint16_t)p[1] << 8;
+}
+
+static inline void write16le(unsigned char *p, uint16_t x)
+{
+ p[0] = x & 255;
+ p[1] = x >> 8 & 255;
+}
+
+static inline uint32_t read32le(unsigned char *p)
+{
+ return (p[0] | (uint32_t)p[1] << 8 |
+ (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24);
+}
+
+static inline void write32le(unsigned char *p, uint32_t x)
+{
+ p[0] = x & 255;
+ p[1] = x >> 8 & 255;
+ p[2] = x >> 16 & 255;
+ p[3] = x >> 24 & 255;
+}
+
+static inline uint64_t read64le(unsigned char *p)
+{
+ return (p[0] | (uint64_t)p[1] << 8 |
+ (uint64_t)p[2] << 16 | (uint64_t)p[3] << 24 |
+ (uint64_t)p[4] << 32 | (uint64_t)p[5] << 40 |
+ (uint64_t)p[6] << 48 | (uint64_t)p[7] << 56);
+}
+
+static inline void write64le(unsigned char *p, uint64_t x)
+{
+ p[0] = x & 255;
+ p[1] = x >> 8 & 255;
+ p[2] = x >> 16 & 255;
+ p[3] = x >> 24 & 255;
+ p[4] = x >> 32 & 255;
+ p[5] = x >> 40 & 255;
+ p[6] = x >> 48 & 255;
+ p[7] = x >> 56 & 255;
+}
+
/* parser debug */
/* #define PARSE_DEBUG */
/* preprocessor debug */