libqbe/lisc/lisc.h

154 lines
1.9 KiB
C
Raw Normal View History

2015-06-29 06:56:11 -04:00
#include <assert.h>
2015-06-11 08:49:34 -04:00
#include <stdio.h>
2015-07-03 12:16:39 -04:00
#include <stdlib.h>
2015-07-15 18:01:38 -04:00
#include <string.h>
2015-06-11 08:49:34 -04:00
2015-07-03 12:16:39 -04:00
typedef unsigned int uint;
2015-07-02 16:06:29 -04:00
typedef unsigned short ushort;
typedef unsigned char uchar;
2015-07-15 18:01:38 -04:00
typedef unsigned long long ullong;
2015-07-02 16:06:29 -04:00
2015-06-11 08:49:34 -04:00
enum {
2015-07-14 06:46:48 -04:00
NReg = 32,
Tmp0 = NReg+1,
2015-07-15 18:01:38 -04:00
2015-07-02 16:06:29 -04:00
NString = 32,
2015-07-14 06:46:48 -04:00
NPred = 15,
NBlk = 128,
NIns = 256,
2015-07-15 18:01:38 -04:00
BITS = 4,
NBit = 8 * sizeof(ullong),
2015-06-11 08:49:34 -04:00
};
2015-07-15 18:01:38 -04:00
typedef struct Bits Bits;
typedef struct Ref Ref;
2015-07-15 02:55:10 -04:00
typedef struct OpDesc OpDesc;
2015-06-11 08:49:34 -04:00
typedef struct Ins Ins;
typedef struct Phi Phi;
typedef struct Blk Blk;
2015-07-03 12:16:39 -04:00
typedef struct Sym Sym;
2015-06-29 06:56:11 -04:00
typedef struct Fn Fn;
2015-07-15 18:01:38 -04:00
struct Bits {
ullong t[BITS];
};
#define BGET(b, n) (1&((b).t[n/NBit]>>(n%NBit)))
#define BSET(b, n) ((b).t[n/NBit] |= (ullong)1<<(n%NBit))
#define BCLR(b, n) ((b).t[n/NBit] &= ~((ullong)1<<(n%NBit)))
2015-07-15 12:41:51 -04:00
struct Ref {
ushort type:1;
ushort val:15;
};
2015-07-15 18:01:38 -04:00
#define R (Ref){0, 0} // Invalid reference
2015-07-15 12:41:51 -04:00
static inline int
req(Ref a, Ref b)
{
return a.type == b.type && a.val == b.val;
}
2015-07-02 16:06:29 -04:00
enum {
2015-07-10 16:17:55 -04:00
RSym = 0,
2015-07-03 12:16:39 -04:00
RConst = 1,
2015-07-15 15:37:26 -04:00
NRef = (1<<15) - 1
2015-07-02 16:06:29 -04:00
};
2015-07-15 15:37:26 -04:00
#define SYM(x) (Ref){RSym, x}
#define CONST(x) (Ref){RConst, x}
2015-06-11 08:49:34 -04:00
2015-07-02 16:06:29 -04:00
enum {
2015-07-15 18:01:38 -04:00
OXXX = 0,
2015-07-02 16:06:29 -04:00
OAdd,
2015-06-11 08:49:34 -04:00
OSub,
2015-06-29 06:56:11 -04:00
ODiv,
2015-06-11 08:49:34 -04:00
OMod,
2015-07-03 15:11:15 -04:00
OCopy,
2015-06-11 08:49:34 -04:00
2015-07-02 16:06:29 -04:00
/* reserved instructions */
OX86Div,
2015-07-15 02:55:10 -04:00
OLast
2015-06-11 08:49:34 -04:00
};
2015-07-02 16:06:29 -04:00
enum {
JXXX,
2015-06-11 08:49:34 -04:00
JRet,
JJmp,
2015-07-03 15:19:36 -04:00
JJez,
2015-06-11 08:49:34 -04:00
};
2015-07-15 18:01:38 -04:00
struct OpDesc {
int arity;
int commut:1;
char *name;
};
2015-06-11 08:49:34 -04:00
struct Ins {
2015-07-02 16:06:29 -04:00
short op;
Ref to;
Ref arg[2];
2015-06-11 08:49:34 -04:00
};
struct Phi {
2015-07-02 16:06:29 -04:00
Ref to;
2015-07-14 06:46:48 -04:00
Ref arg[NPred];
Blk *blk[NPred];
uint narg;
Phi *link;
2015-06-11 08:49:34 -04:00
};
struct Blk {
2015-07-14 06:46:48 -04:00
Phi *phi;
Ins *ins;
uint nins;
2015-06-11 08:49:34 -04:00
struct {
2015-07-02 16:06:29 -04:00
short type;
2015-06-11 08:49:34 -04:00
Ref arg;
} jmp;
2015-07-02 16:06:29 -04:00
Blk *s1;
Blk *s2;
Blk *link;
2015-07-02 16:06:29 -04:00
2015-07-14 06:46:48 -04:00
Blk **pred;
uint npred;
2015-07-15 18:01:38 -04:00
Bits in, out;
2015-07-10 16:17:55 -04:00
char name[NString];
2015-07-14 06:46:48 -04:00
int id;
2015-06-29 06:56:11 -04:00
};
struct Sym {
enum {
SUndef,
SReg,
2015-07-10 16:17:55 -04:00
STmp,
2015-07-02 16:06:29 -04:00
} type;
char name[NString];
int ndef, nuse;
2015-06-29 06:56:11 -04:00
};
struct Fn {
2015-07-02 16:06:29 -04:00
Blk *start;
2015-06-29 06:56:11 -04:00
Sym *sym;
2015-07-10 16:17:55 -04:00
int ntmp;
2015-07-10 13:55:47 -04:00
int nblk;
Blk **rpo;
2015-06-11 08:49:34 -04:00
};
2015-06-29 06:56:11 -04:00
/* parse.c */
2015-07-15 07:15:40 -04:00
extern OpDesc opdesc[];
2015-07-10 11:41:11 -04:00
void *alloc(size_t);
2015-06-29 06:56:11 -04:00
Fn *parsefn(FILE *);
2015-07-15 02:55:10 -04:00
void printfn(Fn *, FILE *);
2015-07-10 11:41:11 -04:00
/* ssa.c */
void fillpreds(Fn *);
2015-07-10 13:55:47 -04:00
void fillrpo(Fn *);
2015-07-10 16:17:55 -04:00
void ssafix(Fn *, int);
2015-07-15 18:01:38 -04:00
/* live.c */
void filllive(Fn *);