diff -rupN tcc-0.9.25/config.h tcc-0.9.25-my/config.h --- tcc-0.9.25/config.h 1970-01-01 02:00:00.000000000 +0200 +++ tcc-0.9.25-my/config.h 2010-11-17 11:22:02.796875000 +0200 @@ -0,0 +1,7 @@ +/* Automatically generated by configure - do not modify */ +#define CONFIG_SYSROOT "" +#define CONFIG_TCCDIR "C:/Program Files/tcc" +#define GCC_MAJOR 3 +#define HOST_I386 1 +#define CONFIG_WIN32 1 +#define TCC_VERSION "0.9.25" diff -rupN tcc-0.9.25/config.mak tcc-0.9.25-my/config.mak --- tcc-0.9.25/config.mak 1970-01-01 02:00:00.000000000 +0200 +++ tcc-0.9.25-my/config.mak 2010-11-17 11:22:02.801757800 +0200 @@ -0,0 +1,21 @@ +# Automatically generated by configure - do not modify +prefix=C:/Program Files/tcc +bindir=C:/Program Files/tcc +tccdir=C:/Program Files/tcc +libdir=C:/Program Files/tcc/lib +includedir=C:/Program Files/tcc/include +mandir=C:/Program Files/tcc/man +docdir=C:/Program Files/tcc/doc +CC=gcc +GCC_MAJOR=3 +HOST_CC=gcc +AR=ar +STRIP=strip -s -R .comment -R .note +CFLAGS=-O2 +LDFLAGS= +LIBSUF=.lib +EXESUF=.exe +ARCH=i386 +CONFIG_WIN32=yes +VERSION=0.9.25 +SRC_PATH=/c/src/tcc-0.9.25 diff -rupN tcc-0.9.25/config.texi tcc-0.9.25-my/config.texi --- tcc-0.9.25/config.texi 1970-01-01 02:00:00.000000000 +0200 +++ tcc-0.9.25-my/config.texi 2010-11-17 11:22:02.798828100 +0200 @@ -0,0 +1 @@ +@set VERSION 0.9.25 diff -rupN tcc-0.9.25/i386-gen.c tcc-0.9.25-my/i386-gen.c --- tcc-0.9.25/i386-gen.c 2009-05-18 17:27:06.000000000 +0300 +++ tcc-0.9.25-my/i386-gen.c 2010-11-22 10:10:04.081054700 +0200 @@ -1,3 +1,10 @@ +#define OBF_PUSH_JMP_INSTEAD_OF_CALL +#define OBF_PROLOG +#define OBF_RET +#define OBF_NOISE_AROUND_OPI +#define OBF_NOISE_AFTER_CALL +#define OBF_LOAD + /* * X86 code generator for TCC * @@ -20,7 +27,6 @@ /* number of available registers */ #define NB_REGS 4 - /* a register can belong to several classes. The classes must be sorted from more general to more precise (see gv2() code which does assumptions on it). */ @@ -89,6 +95,13 @@ static unsigned long func_sub_sp_offset; static unsigned long func_bound_offset; static int func_ret_sub; +// my fn + +void random_noise_to_reg (int r); +void print_vstack (); +int is_reg_in_vstack (int r); +void random_noise_to_regs (int save_EAX); + /* XXX: make it faster ? */ void g(int c) { @@ -183,6 +196,104 @@ static void gen_modrm(int op_reg, int r, } } +void produce_load_sequence(int r, int fc) +{ + int i; + int t=rand_reg (3,6); + int c=rand_reg (0, 7); + unsigned int curval=0; + unsigned int rrr; + + Sym *sym; + + switch (c) + { + case 0: sym = external_global_sym(TOK___noise_const1, &int_type, 0); curval=0x7E4D1944; break; + case 1: sym = external_global_sym(TOK___noise_const2, &int_type, 0); curval=0xE5F3DA1C; break; + case 2: sym = external_global_sym(TOK___noise_const3, &int_type, 0); curval=0x4CE45CAF; break; + case 3: sym = external_global_sym(TOK___noise_const4, &int_type, 0); curval=0xC88F43F7; break; + case 4: sym = external_global_sym(TOK___noise_const5, &int_type, 0); curval=0x833EDD32; break; + case 5: sym = external_global_sym(TOK___noise_const6, &int_type, 0); curval=0x5918E72B; break; + case 6: sym = external_global_sym(TOK___noise_const7, &int_type, 0); curval=0xD19781E9; break; + case 7: sym = external_global_sym(TOK___noise_const8, &int_type, 0); curval=0xD15899C6; break; + }; + + o(0x8b); + o(0 << 6 | r << 3 | 5); // mod reg r/m + gen_le32(0); // disp32 + greloc(cur_text_section, sym, ind-4, R_386_32); +/* + + Sym *sym = external_global_sym(TOK___noise_const2, &int_type, 0); + o(0x8d); // lea edi, noise_const + o(0x3d); + gen_le32(0); // disp32 + greloc(cur_text_section, sym, ind-4, R_386_32); + + o(0x81); // add edi, c*4 + o(0xc7); + gen_le32(c*4); // disp32 + + o(0x8b); // mov r, [edi] + o(0 << 6 | r << 3 | 7); // mod reg r/m + + curval=noise_const_[c]; +*/ + +// rrr=genrand(); curval=rrr; +// o(0xb8 + r); // mov r, rrr +// gen_le32 (rrr); // + + for (i=0; i>rrr; + + o(0xc1); /* shl/shr/sar $xxx, r */ + o(0xe8 | r); + g(rrr); + + break; + + case 4: // xor + + rrr=genrand(); curval=curval^rrr; + o(0x81); oad(0xc0 | (6 << 3) | r, rrr); // xor + break; + }; + }; + + + o(0x81); oad(0xc0 | (5 << 3) | r, curval-fc); // sub +}; /* load 'r' from value 'sv' */ void load(int r, SValue *sv) @@ -226,8 +337,19 @@ void load(int r, SValue *sv) gen_modrm(r, fr, sv->sym, fc); } else { if (v == VT_CONST) { - o(0xb8 + r); /* mov $xx, r */ - gen_addr32(fr, sv->sym, fc); +#ifdef OBF_LOAD + if (fr & VT_SYM) +#endif + { + o(0xb8 + r); /* mov $xx, r */ + gen_addr32(fr, sv->sym, fc); + } +#ifdef OBF_LOAD + else + { + produce_load_sequence(r, fc); + }; +#endif } else if (v == VT_LOCAL) { o(0x8d); /* lea xxx(%ebp), r */ gen_modrm(r, VT_LOCAL, sv->sym, fc); @@ -401,14 +523,34 @@ void gfunc_call(int nb_args) args_size -= 4; } } +#ifdef OBF_PUSH_JMP_INSTEAD_OF_CALL + + oad(0xe8, 0); // call $+5 + o(0x58); // pop eax + o(0x83); o(0xc0); o(0x0a); // add eax, 0xa + o(0x50); // push eax + + gcall_or_jmp(1); +#else gcall_or_jmp(0); +#endif + if (args_size && func_call != FUNC_STDCALL) gadd_sp(args_size); vtop--; +#ifdef OBF_NOISE_AFTER_CALL + random_noise_to_regs(1); +#endif } #ifdef TCC_TARGET_PE + +#ifdef OBF_PROLOG +#define FUNC_PROLOG_SIZE 11 +#else #define FUNC_PROLOG_SIZE 10 +#endif + #else #define FUNC_PROLOG_SIZE 9 #endif @@ -528,7 +670,12 @@ void gfunc_epilog(void) #endif o(0xc9); /* leave */ if (func_ret_sub == 0) { +#ifdef OBF_RET + o(0x5b); // pop ebx + o(0xff); o(0xe3); // jmp ebx +#else o(0xc3); /* ret */ +#endif } else { o(0xc2); /* ret n */ g(func_ret_sub); @@ -548,12 +695,18 @@ void gfunc_epilog(void) } else #endif { +#ifdef OBF_PROLOG + o(0x90); +#endif o(0xe58955); /* push %ebp, mov %esp, %ebp */ o(0xec81); /* sub esp, stacksize */ gen_le32(v); #if FUNC_PROLOG_SIZE == 10 o(0x90); /* adjust to FUNC_PROLOG_SIZE */ #endif +#if FUNC_PROLOG_SIZE == 11 + o(0x90); /* adjust to FUNC_PROLOG_SIZE */ +#endif } ind = saved_ind; } @@ -627,33 +780,21 @@ void gen_opi(int op) { int r, fr, opc, c; +#ifdef OBF_NOISE_AROUND_OPI + random_noise_to_regs(0); +#endif + switch(op) { case '+': case TOK_ADDC1: /* add with carry generation */ opc = 0; gen_op8: - if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { - /* constant case */ - vswap(); - r = gv(RC_INT); - vswap(); - c = vtop->c.i; - if (c == (char)c) { - /* XXX: generate inc and dec for smaller code ? */ - o(0x83); - o(0xc0 | (opc << 3) | r); - g(c); - } else { - o(0x81); - oad(0xc0 | (opc << 3) | r, c); - } - } else { - gv2(RC_INT, RC_INT); - r = vtop[-1].r; - fr = vtop[0].r; - o((opc << 3) | 0x01); - o(0xc0 + r + fr * 8); - } + gv2(RC_INT, RC_INT); + r = vtop[-1].r; + fr = vtop[0].r; + o((opc << 3) | 0x01); // ins ... + o(0xc0 + r + fr * 8); // ... r, fr + vtop--; if (op >= TOK_ULT && op <= TOK_GT) { vtop->r = VT_CMP; @@ -752,6 +893,9 @@ void gen_opi(int op) opc = 7; goto gen_op8; } +#ifdef OBF_NOISE_AROUND_OPI + random_noise_to_regs(0); +#endif } /* generate a floating point operation 'v = t1 op t2' instruction. The @@ -1032,3 +1176,171 @@ void gen_bounded_ptr_deref(void) /* end of X86 code generator */ /*************************************************************/ +void random_LEA_noise_to_reg (int r) +{ + int mod=rand_reg (0,2); + int rm=rand_reg (0,7); + + o(0x8d); // LEA + g((mod<<6) | (r << 3) | rm); // put modrm + //printf ("modrm=%x\n", (mod<<6) | (r << 3) | rm); + + if (mod==1) // add disp8 + { + int disp8 = genrand()&0xFF; + //printf ("adding disp8 0x%x\n", disp8); + g (disp8); + }; + + if (mod==2 || (mod==0 && rm==5)) // add disp32 + { + int disp32=genrand(); + //printf ("adding disp32 0x%x\n", disp32); + gen_le32 (disp32); + }; + + if ((mod==0 || mod==1 || mod==2) && rm==4) // add SIB + { + // SIB is scale(2), index(3), base(3) + int SIB=genrand()&0xFF; + + if ((SIB&7) == 5) // special case + { + //printf ("special case. SIB before=0x%x\n", SIB); + SIB=SIB&0xF8; + } + //printf ("adding SIB 0x%x\n", SIB); + g (SIB); + }; +}; + + +void random_MOV_noise_to_reg (int r) +{ + oad (0xb8 + r, genrand()); // mov r, imm32 +}; + +SValue* vstack_rnd () +{ + int cnt; + int r; + SValue *p; + + //printf ("vstack_rnd begin\n"); + + cnt=0; + for(p=vstack; p<=vtop; p++) + cnt++; + + r=rand_reg (0, cnt); + + cnt=0; + for(p=vstack; p<=vtop; p++) + { + if (cnt==r) + { + //printf ("vstack_rnd return\n"); + return p; + }; + cnt++; + }; + + //printf ("vstack_rnd return NULL\n"); + return NULL; +}; + +void random_noise_to_reg (int r) +{ + //printf ("random_noise_to_reg (%d)\n", r); + + switch (rand_reg(0,3)) + { + case 0: + { + SValue *p; + p=vstack_rnd(); + if (p!=NULL) + load (r, p); + }; + // -> case 1 + + case 1: + { + int opc=rand_reg (0, 6); + int fr=rand_reg (0, 3); + + o((opc << 3) | 0x01); // ins ... + o(0xc0 + r + fr * 8); // ... r, fr + + if ((opc==5 || opc==6) && fr==r) // SUB or XOR + { + g(0x75); // add random JNZ + g(genrand()&0xFF); + }; + + }; + break; + + case 2: + random_LEA_noise_to_reg (r); + break; + + case 3: + random_MOV_noise_to_reg (r); + break; + + }; + +}; + +void print_vstack () +{ + SValue *p; + + printf ("print_vstack()\n"); + for(p=vstack; p<=vtop; p++) + { + printf ("p->r = 0x%x\n", p->r); + }; +}; + +int is_reg_in_vstack (int r) +{ + SValue *p; + + for(p=vstack; p<=vtop; p++) + if (r==((p->r)&0xFF)) + return 1; + + return 0; +}; + +void random_noise_to_regs (int save_EAX) +{ + int i,t; + + //printf ("random_noise_to_regs\n"); + + //print_vstack(); + + t=rand_reg(1,20); + //t=1; + for (i=0; ir == VT_CMP) + { + // BUG? + // if (rand_reg(0,1)==0) + // random_LEA_noise_to_reg (r); + // else + // random_MOV_noise_to_reg (r); + } + else + random_noise_to_reg (r); + }; + }; +}; diff -rupN tcc-0.9.25/lib/libtcc1.c tcc-0.9.25-my/lib/libtcc1.c --- tcc-0.9.25/lib/libtcc1.c 2009-05-18 17:27:06.000000000 +0300 +++ tcc-0.9.25-my/lib/libtcc1.c 2010-11-21 10:00:04.194335900 +0200 @@ -605,3 +605,11 @@ unsigned long long __fixunsxfdi (long do return 0; } +int __noise_const1=0x7E4D1944; +int __noise_const2=0xE5F3DA1C; +int __noise_const3=0x4CE45CAF; +int __noise_const4=0xC88F43F7; +int __noise_const5=0x833EDD32; +int __noise_const6=0x5918E72B; +int __noise_const7=0xD19781E9; +int __noise_const8=0xD15899C6; diff -rupN tcc-0.9.25/lib/libtcc1.c~ tcc-0.9.25-my/lib/libtcc1.c~ --- tcc-0.9.25/lib/libtcc1.c~ 1970-01-01 02:00:00.000000000 +0200 +++ tcc-0.9.25-my/lib/libtcc1.c~ 2009-05-18 17:27:06.000000000 +0300 @@ -0,0 +1,607 @@ +/* TCC runtime library. + Parts of this code are (c) 2002 Fabrice Bellard + + Copyright (C) 1987, 1988, 1992, 1994, 1995 Free Software Foundation, Inc. + +This file is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2, or (at your option) any +later version. + +In addition to the permissions in the GNU General Public License, the +Free Software Foundation gives you unlimited permission to link the +compiled version of this file into combinations with other programs, +and to distribute those combinations without any restriction coming +from the use of this file. (The General Public License restrictions +do apply in other respects; for example, they cover modification of +the file, and distribution when not linked into a combine +executable.) + +This file 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 +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; see the file COPYING. If not, write to +the Free Software Foundation, 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. +*/ + +#define W_TYPE_SIZE 32 +#define BITS_PER_UNIT 8 + +typedef int Wtype; +typedef unsigned int UWtype; +typedef unsigned int USItype; +typedef long long DWtype; +typedef unsigned long long UDWtype; + +struct DWstruct { + Wtype low, high; +}; + +typedef union +{ + struct DWstruct s; + DWtype ll; +} DWunion; + +typedef long double XFtype; +#define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT) +#define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE) + +/* the following deal with IEEE single-precision numbers */ +#define EXCESS 126 +#define SIGNBIT 0x80000000 +#define HIDDEN (1 << 23) +#define SIGN(fp) ((fp) & SIGNBIT) +#define EXP(fp) (((fp) >> 23) & 0xFF) +#define MANT(fp) (((fp) & 0x7FFFFF) | HIDDEN) +#define PACK(s,e,m) ((s) | ((e) << 23) | (m)) + +/* the following deal with IEEE double-precision numbers */ +#define EXCESSD 1022 +#define HIDDEND (1 << 20) +#define EXPD(fp) (((fp.l.upper) >> 20) & 0x7FF) +#define SIGND(fp) ((fp.l.upper) & SIGNBIT) +#define MANTD(fp) (((((fp.l.upper) & 0xFFFFF) | HIDDEND) << 10) | \ + (fp.l.lower >> 22)) +#define HIDDEND_LL ((long long)1 << 52) +#define MANTD_LL(fp) ((fp.ll & (HIDDEND_LL-1)) | HIDDEND_LL) +#define PACKD_LL(s,e,m) (((long long)((s)+((e)<<20))<<32)|(m)) + +/* the following deal with x86 long double-precision numbers */ +#define EXCESSLD 16382 +#define EXPLD(fp) (fp.l.upper & 0x7fff) +#define SIGNLD(fp) ((fp.l.upper) & 0x8000) + +/* only for x86 */ +union ldouble_long { + long double ld; + struct { + unsigned long long lower; + unsigned short upper; + } l; +}; + +union double_long { + double d; +#if 1 + struct { + unsigned int lower; + int upper; + } l; +#else + struct { + int upper; + unsigned int lower; + } l; +#endif + long long ll; +}; + +union float_long { + float f; + long l; +}; + +/* XXX: we don't support several builtin supports for now */ +#ifndef __x86_64__ + +/* XXX: use gcc/tcc intrinsic ? */ +#if defined(__i386__) +#define sub_ddmmss(sh, sl, ah, al, bh, bl) \ + __asm__ ("subl %5,%1\n\tsbbl %3,%0" \ + : "=r" ((USItype) (sh)), \ + "=&r" ((USItype) (sl)) \ + : "0" ((USItype) (ah)), \ + "g" ((USItype) (bh)), \ + "1" ((USItype) (al)), \ + "g" ((USItype) (bl))) +#define umul_ppmm(w1, w0, u, v) \ + __asm__ ("mull %3" \ + : "=a" ((USItype) (w0)), \ + "=d" ((USItype) (w1)) \ + : "%0" ((USItype) (u)), \ + "rm" ((USItype) (v))) +#define udiv_qrnnd(q, r, n1, n0, dv) \ + __asm__ ("divl %4" \ + : "=a" ((USItype) (q)), \ + "=d" ((USItype) (r)) \ + : "0" ((USItype) (n0)), \ + "1" ((USItype) (n1)), \ + "rm" ((USItype) (dv))) +#define count_leading_zeros(count, x) \ + do { \ + USItype __cbtmp; \ + __asm__ ("bsrl %1,%0" \ + : "=r" (__cbtmp) : "rm" ((USItype) (x))); \ + (count) = __cbtmp ^ 31; \ + } while (0) +#else +#error unsupported CPU type +#endif + +/* most of this code is taken from libgcc2.c from gcc */ + +static UDWtype __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp) +{ + DWunion ww; + DWunion nn, dd; + DWunion rr; + UWtype d0, d1, n0, n1, n2; + UWtype q0, q1; + UWtype b, bm; + + nn.ll = n; + dd.ll = d; + + d0 = dd.s.low; + d1 = dd.s.high; + n0 = nn.s.low; + n1 = nn.s.high; + +#if !UDIV_NEEDS_NORMALIZATION + if (d1 == 0) + { + if (d0 > n1) + { + /* 0q = nn / 0D */ + + udiv_qrnnd (q0, n0, n1, n0, d0); + q1 = 0; + + /* Remainder in n0. */ + } + else + { + /* qq = NN / 0d */ + + if (d0 == 0) + d0 = 1 / d0; /* Divide intentionally by zero. */ + + udiv_qrnnd (q1, n1, 0, n1, d0); + udiv_qrnnd (q0, n0, n1, n0, d0); + + /* Remainder in n0. */ + } + + if (rp != 0) + { + rr.s.low = n0; + rr.s.high = 0; + *rp = rr.ll; + } + } + +#else /* UDIV_NEEDS_NORMALIZATION */ + + if (d1 == 0) + { + if (d0 > n1) + { + /* 0q = nn / 0D */ + + count_leading_zeros (bm, d0); + + if (bm != 0) + { + /* Normalize, i.e. make the most significant bit of the + denominator set. */ + + d0 = d0 << bm; + n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm)); + n0 = n0 << bm; + } + + udiv_qrnnd (q0, n0, n1, n0, d0); + q1 = 0; + + /* Remainder in n0 >> bm. */ + } + else + { + /* qq = NN / 0d */ + + if (d0 == 0) + d0 = 1 / d0; /* Divide intentionally by zero. */ + + count_leading_zeros (bm, d0); + + if (bm == 0) + { + /* From (n1 >= d0) /\ (the most significant bit of d0 is set), + conclude (the most significant bit of n1 is set) /\ (the + leading quotient digit q1 = 1). + + This special case is necessary, not an optimization. + (Shifts counts of W_TYPE_SIZE are undefined.) */ + + n1 -= d0; + q1 = 1; + } + else + { + /* Normalize. */ + + b = W_TYPE_SIZE - bm; + + d0 = d0 << bm; + n2 = n1 >> b; + n1 = (n1 << bm) | (n0 >> b); + n0 = n0 << bm; + + udiv_qrnnd (q1, n1, n2, n1, d0); + } + + /* n1 != d0... */ + + udiv_qrnnd (q0, n0, n1, n0, d0); + + /* Remainder in n0 >> bm. */ + } + + if (rp != 0) + { + rr.s.low = n0 >> bm; + rr.s.high = 0; + *rp = rr.ll; + } + } +#endif /* UDIV_NEEDS_NORMALIZATION */ + + else + { + if (d1 > n1) + { + /* 00 = nn / DD */ + + q0 = 0; + q1 = 0; + + /* Remainder in n1n0. */ + if (rp != 0) + { + rr.s.low = n0; + rr.s.high = n1; + *rp = rr.ll; + } + } + else + { + /* 0q = NN / dd */ + + count_leading_zeros (bm, d1); + if (bm == 0) + { + /* From (n1 >= d1) /\ (the most significant bit of d1 is set), + conclude (the most significant bit of n1 is set) /\ (the + quotient digit q0 = 0 or 1). + + This special case is necessary, not an optimization. */ + + /* The condition on the next line takes advantage of that + n1 >= d1 (true due to program flow). */ + if (n1 > d1 || n0 >= d0) + { + q0 = 1; + sub_ddmmss (n1, n0, n1, n0, d1, d0); + } + else + q0 = 0; + + q1 = 0; + + if (rp != 0) + { + rr.s.low = n0; + rr.s.high = n1; + *rp = rr.ll; + } + } + else + { + UWtype m1, m0; + /* Normalize. */ + + b = W_TYPE_SIZE - bm; + + d1 = (d1 << bm) | (d0 >> b); + d0 = d0 << bm; + n2 = n1 >> b; + n1 = (n1 << bm) | (n0 >> b); + n0 = n0 << bm; + + udiv_qrnnd (q0, n1, n2, n1, d1); + umul_ppmm (m1, m0, q0, d0); + + if (m1 > n1 || (m1 == n1 && m0 > n0)) + { + q0--; + sub_ddmmss (m1, m0, m1, m0, d1, d0); + } + + q1 = 0; + + /* Remainder in (n1n0 - m1m0) >> bm. */ + if (rp != 0) + { + sub_ddmmss (n1, n0, n1, n0, m1, m0); + rr.s.low = (n1 << b) | (n0 >> bm); + rr.s.high = n1 >> bm; + *rp = rr.ll; + } + } + } + } + + ww.s.low = q0; + ww.s.high = q1; + return ww.ll; +} + +#define __negdi2(a) (-(a)) + +long long __divdi3(long long u, long long v) +{ + int c = 0; + DWunion uu, vv; + DWtype w; + + uu.ll = u; + vv.ll = v; + + if (uu.s.high < 0) { + c = ~c; + uu.ll = __negdi2 (uu.ll); + } + if (vv.s.high < 0) { + c = ~c; + vv.ll = __negdi2 (vv.ll); + } + w = __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) 0); + if (c) + w = __negdi2 (w); + return w; +} + +long long __moddi3(long long u, long long v) +{ + int c = 0; + DWunion uu, vv; + DWtype w; + + uu.ll = u; + vv.ll = v; + + if (uu.s.high < 0) { + c = ~c; + uu.ll = __negdi2 (uu.ll); + } + if (vv.s.high < 0) + vv.ll = __negdi2 (vv.ll); + + __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) &w); + if (c) + w = __negdi2 (w); + return w; +} + +unsigned long long __udivdi3(unsigned long long u, unsigned long long v) +{ + return __udivmoddi4 (u, v, (UDWtype *) 0); +} + +unsigned long long __umoddi3(unsigned long long u, unsigned long long v) +{ + UDWtype w; + + __udivmoddi4 (u, v, &w); + return w; +} + +/* XXX: fix tcc's code generator to do this instead */ +long long __ashrdi3(long long a, int b) +{ +#ifdef __TINYC__ + DWunion u; + u.ll = a; + if (b >= 32) { + u.s.low = u.s.high >> (b - 32); + u.s.high = u.s.high >> 31; + } else if (b != 0) { + u.s.low = ((unsigned)u.s.low >> b) | (u.s.high << (32 - b)); + u.s.high = u.s.high >> b; + } + return u.ll; +#else + return a >> b; +#endif +} + +/* XXX: fix tcc's code generator to do this instead */ +unsigned long long __lshrdi3(unsigned long long a, int b) +{ +#ifdef __TINYC__ + DWunion u; + u.ll = a; + if (b >= 32) { + u.s.low = (unsigned)u.s.high >> (b - 32); + u.s.high = 0; + } else if (b != 0) { + u.s.low = ((unsigned)u.s.low >> b) | (u.s.high << (32 - b)); + u.s.high = (unsigned)u.s.high >> b; + } + return u.ll; +#else + return a >> b; +#endif +} + +/* XXX: fix tcc's code generator to do this instead */ +long long __ashldi3(long long a, int b) +{ +#ifdef __TINYC__ + DWunion u; + u.ll = a; + if (b >= 32) { + u.s.high = (unsigned)u.s.low << (b - 32); + u.s.low = 0; + } else if (b != 0) { + u.s.high = ((unsigned)u.s.high << b) | ((unsigned)u.s.low >> (32 - b)); + u.s.low = (unsigned)u.s.low << b; + } + return u.ll; +#else + return a << b; +#endif +} + +#if defined(__i386__) +/* FPU control word for rounding to nearest mode */ +unsigned short __tcc_fpu_control = 0x137f; +/* FPU control word for round to zero mode for int conversion */ +unsigned short __tcc_int_fpu_control = 0x137f | 0x0c00; +#endif + +#endif /* !__x86_64__ */ + +/* XXX: fix tcc's code generator to do this instead */ +float __floatundisf(unsigned long long a) +{ + DWunion uu; + XFtype r; + + uu.ll = a; + if (uu.s.high >= 0) { + return (float)uu.ll; + } else { + r = (XFtype)uu.ll; + r += 18446744073709551616.0; + return (float)r; + } +} + +double __floatundidf(unsigned long long a) +{ + DWunion uu; + XFtype r; + + uu.ll = a; + if (uu.s.high >= 0) { + return (double)uu.ll; + } else { + r = (XFtype)uu.ll; + r += 18446744073709551616.0; + return (double)r; + } +} + +long double __floatundixf(unsigned long long a) +{ + DWunion uu; + XFtype r; + + uu.ll = a; + if (uu.s.high >= 0) { + return (long double)uu.ll; + } else { + r = (XFtype)uu.ll; + r += 18446744073709551616.0; + return (long double)r; + } +} + +unsigned long long __fixunssfdi (float a1) +{ + register union float_long fl1; + register int exp; + register unsigned long l; + + fl1.f = a1; + + if (fl1.l == 0) + return (0); + + exp = EXP (fl1.l) - EXCESS - 24; + + l = MANT(fl1.l); + if (exp >= 41) + return (unsigned long long)-1; + else if (exp >= 0) + return (unsigned long long)l << exp; + else if (exp >= -23) + return l >> -exp; + else + return 0; +} + +unsigned long long __fixunsdfdi (double a1) +{ + register union double_long dl1; + register int exp; + register unsigned long long l; + + dl1.d = a1; + + if (dl1.ll == 0) + return (0); + + exp = EXPD (dl1) - EXCESSD - 53; + + l = MANTD_LL(dl1); + + if (exp >= 12) + return (unsigned long long)-1; + else if (exp >= 0) + return l << exp; + else if (exp >= -52) + return l >> -exp; + else + return 0; +} + +unsigned long long __fixunsxfdi (long double a1) +{ + register union ldouble_long dl1; + register int exp; + register unsigned long long l; + + dl1.ld = a1; + + if (dl1.l.lower == 0 && dl1.l.upper == 0) + return (0); + + exp = EXPLD (dl1) - EXCESSLD - 64; + + l = dl1.l.lower; + + if (exp > 0) + return (unsigned long long)-1; + else if (exp >= -63) + return l >> -exp; + else + return 0; +} + diff -rupN tcc-0.9.25/libtcc.c tcc-0.9.25-my/libtcc.c --- tcc-0.9.25/libtcc.c 2009-05-18 17:27:06.000000000 +0300 +++ tcc-0.9.25-my/libtcc.c 2010-11-18 07:50:43.968750000 +0200 @@ -2257,3 +2257,86 @@ void tcc_print_stats(TCCState *s, int64_ tt, (int)(total_lines / tt), total_bytes / tt / 1000000.0); } + +// rand + +#include + +/* Period parameters */ +#define N 624 +#define M 397 +#define MATRIX_A 0x9908b0df /* constant vector a */ +#define UPPER_MASK 0x80000000 /* most significant w-r bits */ +#define LOWER_MASK 0x7fffffff /* least significant r bits */ + +/* Tempering parameters */ +#define TEMPERING_MASK_B 0x9d2c5680 +#define TEMPERING_MASK_C 0xefc60000 +#define TEMPERING_SHIFT_U(y) (y >> 11) +#define TEMPERING_SHIFT_S(y) (y << 7) +#define TEMPERING_SHIFT_T(y) (y << 15) +#define TEMPERING_SHIFT_L(y) (y >> 18) + +static unsigned long mt[N]; /* the array for the state vector */ +static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */ + +/* initializing the array with a NONZERO seed */ +void sgenrand (unsigned long seed) +{ + /* setting initial seeds to mt[N] using */ + /* the generator Line 25 of Table 1 in */ + /* [KNUTH 1981, The Art of Computer Programming */ + /* Vol. 2 (2nd Ed.), pp102] */ + mt[0]= seed & 0xffffffff; + for (mti=1; mti= N) { /* generate N words at one time */ + int kk; + + if (mti == N+1) /* if sgenrand() has not been called, */ + sgenrand(4357); /* a default initial seed is used */ + + for (kk=0;kk> 1) ^ mag01[y & 0x1]; + } + for (;kk> 1) ^ mag01[y & 0x1]; + } + y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); + mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1]; + + mti = 0; + } + + y = mt[mti++]; + y ^= TEMPERING_SHIFT_U(y); + y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B; + y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C; + y ^= TEMPERING_SHIFT_L(y); + + return y; +} + +int rand_reg (int begin, int end) +{ + if (end