This repository has been archived by the owner on Jul 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uwulang.cpp
361 lines (328 loc) · 10.4 KB
/
uwulang.cpp
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* UwULang, an esoteric programming language with emojis
* More info on https://uwulang.com
* Version 0.1.1
* License: MIT
*/
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <argp.h>
#include "llvm/ADT/Optional.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
// program version
#define VERSION "0.1.1"
static char DOC[] = "UwULang, an esoteric programming language with emojis";
const char *argp_program_version = VERSION;
static llvm::LLVMContext Context;
static llvm:: IRBuilder<> Builder(Context);
static std::unique_ptr<llvm:Module> Module; // so we don't have to free it
/**
* Moves current block.
*
* When we need to jump to the next or previous block on the tape, we
* need to emit a jump instruction.
*
* @param ptr Pointer to the current block.
* @param amount Amount of block to move (-1 or +1)
* @return
*/
void emit_move_ptr(llvm::Value* ptr, int amount) {
Builder.CreateStore(
Builder.CreateGEP(
Builder.getInt8Ty(),
Builder.CreateLoad(ptr),
Builder.getInt32(amount)
),
ptr
)
}
/**
* Increments the value of the current block.
*
* @param ptr Pointer to the current block.
* @param amount Amount to increment the value by (-1 or +1)
* @return
*/
void emit_value(llvm::Value* ptr, int amount) {
// get the value of the current block
llvm::Value* val = Builder.CreateLoad(ptr);
Builder.CreateStore(
Builder.CreateAdd(
Builder.CreateLoad(val),
Builder.getInt8(amount)
),
val
);
}
/* IO sys calls */
/**
* Prints the current block.
*
* @param ptr Pointer to the current block.
* @return
*/
void emit_print(llvm::Value* ptr) {
llvm::Function* printChar = llvm::cast<llvm::Function>(
Module->getOrInsertFunction(
"putchar",
Builder.getInt32Ty(),
Builder.getInt32Ty(),
nullptr
)
);
// sys call to print the current block
Builder.CreateCall(
printChar,
Builder.CreateSExt(
Builder.CreateLoad(ptr),
Builder.getInt32Ty()
)
);
}
/**
* Reads a char from stdin and stores it in the current block.
*
* @param ptr Pointer to the current block.
* @return
*/
void emit_read(llvm::Value* ptr) {
llvm::Function* readChar = llvm::cast<llvm::Function>(
Module->getOrInsertFunction(
"getchar",
Builder.getInt32Ty(),
nullptr
)
);
// sys call to read a char from stdin
Builder.CreateStore(
Builder.CreateTrunc(
Builder.CreateCall(readChar),
Builder.getInt8Ty()
),
ptr
);
}
/* loops - think of loop varients as while (curr) */
struct WhileBlock {
llvm::BasicBlock* cond_block;
llvm::BasicBlock* body_block;
llvm::BasicBlock* end_block;
};
/**
* Emits the beginning of a while loop.
*
* @param func Function to emit the loop in.
* @param ptr Pointer to the current block.
* @param while_block Pointer to the while block struct.
* @param while_index Index of the while loop.
* @return
*/
void emit_while_begin(llvm::Function* func, llvm::Value* ptr, WhileBlock* while_block, int while_index) {
while_block->cond_block = llvm::BasicBlock::Create(
Context, std::string("while_cond") + std::to_string(while_index), func);
while_block->body_block = llvm::BasicBlock::Create(
Context, std::string("while_body") + std::to_string(while_index), func);
while_block->end_block = llvm::BasicBlock::Create(
Context, std::string("while_end") + std::to_string(while_index), func);
Builder.CreateBr(while_block->cond_block);
Builder.SetInsertPoint(while_block->cond_block);
Builder.CreateCondBr(
Builder.CreateICmpNE(
Builder.CreateLoad(Builder.CreateLoad(ptr)),
Builder.getInt8(0)),
while_block->body_block,
while_block->end_block);
Builder.SetInsertPoint(while_block->body_block);
}
/**
* Emits the end of a while loop.
*
* @param while_block Pointer to the while block struct.
* @return
*/
void emit_while_end(WhileBlock* while_block) {
Builder.CreateBr(while_block->cond_block);
Builder.SetInsertPoint(while_block->end_block);
}
int compile_uwulang(int charCounter, unsigned char code[]) {
// initalization of LLVM
Module = llvm::make_unique<llvm::Module>("top", Context);
llvm::Function* mainFunc = llvm::Function::Create(
llvm::FunctionType::get(llvm::Type::getInt32Ty(Context), false),
llvm::Function::ExternalLinkage, "main", Module.get());
Builder.SetInsertPoint(llvm::BasicBlock::Create(Context, "", mainFunc));
llvm::Value* data = Builder.CreateAlloca(Builder.getInt8PtrTy(), nullptr, "data");
llvm::Value* ptr = Builder.CreateAlloca(Builder.getInt8PtrTy(), nullptr, "ptr");
llvm::Function* funcCalloc = llvm::cast<llvm::Function>(
Module->getOrInsertFunction("calloc",
Builder.getInt8PtrTy(),
Builder.getInt64Ty(), Builder.getInt64Ty(),
nullptr));
llvm::Value* data_ptr = Builder.CreateCall(funcCalloc, {Builder.getInt64(30000), Builder.getInt64(1)});
Builder.CreateStore(data_ptr, data);
Builder.CreateStore(data_ptr, ptr);
int while_index = 0;
WhileBlock while_blocks[1000];
WhileBlock* while_block_ptr = while_blocks;
// get the input
// TODO:
int _ptr = 0;
int codePtr = 0;
int currChar, nextChar;
while (code[_ptr] != EOF && code[_ptr] != '\0') {
// if not emoji continue
if (code[_ptr] != 240) {
_ptr++;
continue;
}
_ptr++;
if (code[_ptr] != 159) {
ptr++;
continue;
}
_ptr++;
currChar = code[_ptr];
nextChar = code[_ptr+1];
if (currChar == 145 && nextChar == 134) {
emit_value(ptr, 1);
}
else if (currChar == 145 && nextChar == 135) {
emit_value(ptr, -1);
}
else if (currChar == 165 && nextChar == 186) {
emit_print(ptr);
}
else if (currChar == 152 && nextChar == 179)
emit_read(ptr);
else if (currChar == 145 && nextChar == 137)
emit_move_ptr(ptr, 1);
else if (currChar == 145 && nextChar == 136) {
emit_move_ptr(ptr, -1);
} else if (currChar == 152 && nextChar == 146) {
emit_while_begin(mainFunc, ptr, while_block_ptr++, while_index++);
} else if (currChar == 152 && nextChar == 161) {
// 😡 ]
if (--while_block_ptr < while_blocks) {
std::cerr << "unmatching 😡\n";
return 1;
}
}
ptr+=2;
codePtr++;
}
// build the AST
llvm::Function* funcFree = llvm::cast<llvm::Function>(
Module->getOrInsertFunction("free",
Builder.getVoidTy(),
Builder.getInt8PtrTy(),
nullptr));
Builder.CreateCall(funcFree, {Builder.CreateLoad(data)});
Builder.CreateRet(Builder.getInt32(0));
llvm::InitializeAllTargetInfos();
llvm::InitializeAllTargets();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmParsers();
llvm::InitializeAllAsmPrinters();
std::string TargetTriple = llvm::sys::getDefaultTargetTriple();
std::string err;
const llvm::Target* Target = llvm::TargetRegistry::lookupTarget(TargetTriple, err);
if (!Target) {
std::cerr << "Failed to lookup target " + TargetTriple + ": " + err;
return 1;
}
llvm::TargetOptions opt;
llvm::TargetMachine* TheTargetMachine = Target->createTargetMachine(
TargetTriple, "generic", "", opt, llvm::Optional<llvm::Reloc::Model>());
Module->setTargetTriple(TargetTriple);
Module->setDataLayout(TheTargetMachine->createDataLayout());
std::string Filename = "output.o";
std::error_code err_code;
llvm::raw_fd_ostream dest(Filename, err_code, llvm::sys::fs::OF_None);
if (err_code) {
std::cerr << "Could not open file: " << err_code.message();
return 1;
}
llvm::legacy::PassManager pass;
if (TheTargetMachine->addPassesToEmitFile(pass, dest, llvm::TargetMachine::CGFT_ObjectFile)) {
std::cerr << "TheTargetMachine can't emit a file of this type\n";
return 1;
}
pass.run(*Module);
dest.flush();
std::cout << "Wrote " << Filename << "\n";
return 0;
}
int file_entry(char* filename) {
// inital way
unsigned char code[32768] = {0};
int charCounter = 0;
FILE *fp;
// open file
fp = fopen(filename, "r");
if (!fp) {
printf("Error in opening file!\n");
return 1;
}
fscanf(fp, "%[^=]=%[^;]; ", code);
charCounter = strlen((char*)code);
fclose(fp);
compile_uwulang(charCounter, code);
return 0;
}
static int parse_opt(int key, char *arg, struct argp_state *state) {
switch (key) {
case 'v':
printf("UwULang %s\n", VERSION);
break;
// get from file
case 'f':
case 'l':
// print arg
// printf("File: %s\n", arg);
if (file_entry(arg)) {
return 1;
}
break;
case ARGP_KEY_ARG:
if (state->arg_num == 0) {
// First argument is provided
file_entry(arg);
} else {
// Too many arguments
printf("Too many arguments provided\n");
}
break;
// case ARGP_KEY_END:
// if (state->arg_num < 1) {
// // No arguments provided, use default
// cin_entry();
// }
// break;
}
return 0;
}
/// Main takes in either a single file with file extension uwu
/// or if no args are present will run interpreted on the command line
int main(int argc, char *argv[]) {
// no buffer when printing
setbuf(stdout, NULL);
struct argp_option options[] = {
{ "file", 'f', "FILE", 0, ".uwu input file"},
{ "filename", 'l', "FILE", 0, ""},
{ 0 }
};
struct argp argp = { options, parse_opt, 0, DOC };
return argp_parse(&argp, argc, argv, 0, 0, 0);
}