This repository was archived by the owner on Feb 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcompiler.c
212 lines (185 loc) · 4.23 KB
/
compiler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "CgDrv.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
static int wrap_log(const char *format, ...)
{
va_list args;
int ret;
va_start(args, format);
ret = vfprintf(stdout, format, args);
va_end(args);
return ret;
}
void hexdump(const void *data, int size)
{
unsigned char *buf = (void *) data;
char alpha[17];
int i;
for (i = 0; i < size; i++) {
/* if (!(i % 16))
wrap_log("\t\t%08X", (unsigned int) buf + i); */
if (((void *) (buf + i)) < ((void *) data)) {
wrap_log(" ");
alpha[i % 16] = '.';
} else {
wrap_log(" %02X", buf[i]);
if (isprint(buf[i]) && (buf[i] < 0xA0))
alpha[i % 16] = buf[i];
else
alpha[i % 16] = '.';
}
if ((i % 16) == 15) {
alpha[16] = 0;
wrap_log("\t|%s|\n", alpha);
}
}
if (i % 16) {
for (i %= 16; i < 16; i++) {
wrap_log(" ");
alpha[i] = '.';
if (i == 15) {
alpha[16] = 0;
wrap_log("\t|%s|\n", alpha);
}
}
}
}
static void dump_ctx(struct CgCtx *ctx)
{
int i;
wrap_log("unknown00 = %08x\n", ctx->unknown00);
wrap_log("error: %s\n", ctx->error ? ctx->error : "<no error>");
wrap_log("log: %s\n", ctx->log ? ctx->log : "<no log>");
wrap_log("binary (%d bytes) =\n", ctx->binary_size);
hexdump(ctx->binary, ctx->binary_size);
wrap_log("unknown20 = %08x\n", ctx->unknown20);
wrap_log("unknown24 = %p\n", ctx->unknown24);
wrap_log("unknown28 = %zu\n", ctx->unknown28);
hexdump(ctx->unknown24, ctx->unknown28);
wrap_log("unknown32 = %p\n", ctx->unknown32);
wrap_log("unknown36 = %08x\n", ctx->unknown36);
wrap_log("unknown40 = %08x\n", ctx->unknown40);
wrap_log("unknown44 = &unknown%d (%p)\n",
(int)ctx->unknown44 - (int)ctx, ctx->unknown44);
wrap_log("unknown48 = %08x\n", ctx->unknown48);
wrap_log("unknown52 = %08x\n", ctx->unknown52);
// wrap_log("unknown56 = %p\n", ctx->unknown56);
// hexdump(ctx->unknown56, 0x100);
wrap_log("unknown60 = %08x\n", ctx->unknown60);
wrap_log("unknown64 = %08x\n", ctx->unknown64);
wrap_log("unknown68 = %08x\n", ctx->unknown68);
wrap_log("unknown72 = %08x\n", ctx->unknown72);
wrap_log("unknown76 = %08x\n", ctx->unknown76);
}
int main(int argc, char *argv[])
{
int i, ret, dump = 0;
enum shader_type type = FRAGMENT;
for (i = 1; i < argc; ++i) {
struct CgCtx *ctx;
#ifndef WANT_STABLE_ADDRS
char *src;
size_t src_len, src_alloc;
#else
static char src[0x10000];
size_t src_len;
#endif
FILE *fp;
const char *arg = argv[i];
if (!strcmp(arg, "--frag")) {
type = FRAGMENT;
continue;
}
if (!strcmp(arg, "--vert")) {
type = VERTEX;
continue;
}
if (!strcmp(arg, "--dump")) {
dump = 1;
continue;
}
fp = fopen(arg, "r");
if (!fp) {
perror("fopen");
exit(1);
}
#ifndef WANT_STABLE_ADDRS
src_alloc = 100;
src = malloc(src_alloc);
src_len = 0;
while (1) {
size_t read = fread(src + src_len, 1,
src_alloc - src_len, fp);
if (!read)
break;
src_len += read;
src_alloc = src_alloc * 2;
src = realloc(src, src_alloc);
if (!src) {
perror("realloc");
exit(1);
}
}
fclose(fp);
src = realloc(src, src_len + 1);
if (!src) {
perror("realloc");
exit(1);
}
#else
src_len = fread(src, 1, sizeof(src) - 1, fp);
#endif
src[src_len] = '\0';
ctx = CgDrv_Create();
if (!ctx) {
fprintf(stderr, "CgDrv_Create failed!\n");
return -1;
}
ret = CgDrv_Compile(ctx, 1, type, src, src_len, 0);
if (ret) {
fprintf(stderr, "CgDrv_Compile returned %d\n", ret);
if (ctx->error)
fprintf(stderr, "%s", ctx->error);
return -1;
}
if (dump)
dump_ctx(ctx);
if (type == FRAGMENT)
fp = fopen("out.nvbf", "wb");
else
fp = fopen("out.nvbv", "wb");
if (fp) {
fwrite(ctx->binary, 1, ctx->binary_size, fp);
fclose(fp);
}
}
return 0;
}
#if 0
extern void *__libc_malloc(size_t size);
void *malloc(size_t size)
{
void *ret;
wrap_log("malloc(0x%x)", size);
ret = __libc_malloc(size);
wrap_log(" = %p\n", ret);
return ret;
}
extern void __libc_free(void *ptr);
void free(void *ptr)
{
wrap_log("free(%p)\n", ptr);
__libc_free(ptr);
}
extern void *__libc_realloc(void *ptr, size_t size);
void *realloc(void *ptr, size_t size)
{
void *ret;
wrap_log("realloc(%p, 0x%x)", ptr, size);
ret = __libc_realloc(ptr, size);
wrap_log(" = %p\n", ret);
return ret;
}
#endif