BrainF.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. //===-- BrainF.cpp - BrainF compiler example ------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This class compiles the BrainF language into LLVM assembly.
  10. //
  11. // The BrainF language has 8 commands:
  12. // Command Equivalent C Action
  13. // ------- ------------ ------
  14. // , *h=getchar(); Read a character from stdin, 255 on EOF
  15. // . putchar(*h); Write a character to stdout
  16. // - --*h; Decrement tape
  17. // + ++*h; Increment tape
  18. // < --h; Move head left
  19. // > ++h; Move head right
  20. // [ while(*h) { Start loop
  21. // ] } End loop
  22. //
  23. //===----------------------------------------------------------------------===//
  24. #include "BrainF.h"
  25. #include "llvm/ADT/APInt.h"
  26. #include "llvm/IR/BasicBlock.h"
  27. #include "llvm/IR/Constant.h"
  28. #include "llvm/IR/Constants.h"
  29. #include "llvm/IR/DerivedTypes.h"
  30. #include "llvm/IR/Function.h"
  31. #include "llvm/IR/GlobalValue.h"
  32. #include "llvm/IR/GlobalVariable.h"
  33. #include "llvm/IR/InstrTypes.h"
  34. #include "llvm/IR/Instruction.h"
  35. #include "llvm/IR/Instructions.h"
  36. #include "llvm/IR/Intrinsics.h"
  37. #include "llvm/IR/Module.h"
  38. #include "llvm/IR/Type.h"
  39. #include "llvm/Support/Casting.h"
  40. #include <cstdlib>
  41. #include <iostream>
  42. using namespace llvm;
  43. //Set the constants for naming
  44. const char *BrainF::tapereg = "tape";
  45. const char *BrainF::headreg = "head";
  46. const char *BrainF::label = "brainf";
  47. const char *BrainF::testreg = "test";
  48. Module *BrainF::parse(std::istream *in1, int mem, CompileFlags cf,
  49. LLVMContext& Context) {
  50. in = in1;
  51. memtotal = mem;
  52. comflag = cf;
  53. header(Context);
  54. readloop(nullptr, nullptr, nullptr, Context);
  55. delete builder;
  56. return module;
  57. }
  58. void BrainF::header(LLVMContext& C) {
  59. module = new Module("BrainF", C);
  60. //Function prototypes
  61. //declare void @llvm.memset.p0i8.i32(i8 *, i8, i32, i32, i1)
  62. Type *Tys[] = { Type::getInt8PtrTy(C), Type::getInt32Ty(C) };
  63. Function *memset_func = Intrinsic::getDeclaration(module, Intrinsic::memset,
  64. Tys);
  65. //declare i32 @getchar()
  66. getchar_func = cast<Function>(module->
  67. getOrInsertFunction("getchar", IntegerType::getInt32Ty(C)));
  68. //declare i32 @putchar(i32)
  69. putchar_func = cast<Function>(module->
  70. getOrInsertFunction("putchar", IntegerType::getInt32Ty(C),
  71. IntegerType::getInt32Ty(C)));
  72. //Function header
  73. //define void @brainf()
  74. brainf_func = cast<Function>(module->
  75. getOrInsertFunction("brainf", Type::getVoidTy(C)));
  76. builder = new IRBuilder<>(BasicBlock::Create(C, label, brainf_func));
  77. //%arr = malloc i8, i32 %d
  78. ConstantInt *val_mem = ConstantInt::get(C, APInt(32, memtotal));
  79. BasicBlock* BB = builder->GetInsertBlock();
  80. Type* IntPtrTy = IntegerType::getInt32Ty(C);
  81. Type* Int8Ty = IntegerType::getInt8Ty(C);
  82. Constant* allocsize = ConstantExpr::getSizeOf(Int8Ty);
  83. allocsize = ConstantExpr::getTruncOrBitCast(allocsize, IntPtrTy);
  84. ptr_arr = CallInst::CreateMalloc(BB, IntPtrTy, Int8Ty, allocsize, val_mem,
  85. nullptr, "arr");
  86. BB->getInstList().push_back(cast<Instruction>(ptr_arr));
  87. //call void @llvm.memset.p0i8.i32(i8 *%arr, i8 0, i32 %d, i32 1, i1 0)
  88. {
  89. Value *memset_params[] = {
  90. ptr_arr,
  91. ConstantInt::get(C, APInt(8, 0)),
  92. val_mem,
  93. ConstantInt::get(C, APInt(32, 1)),
  94. ConstantInt::get(C, APInt(1, 0))
  95. };
  96. CallInst *memset_call = builder->
  97. CreateCall(memset_func, memset_params);
  98. memset_call->setTailCall(false);
  99. }
  100. //%arrmax = getelementptr i8 *%arr, i32 %d
  101. if (comflag & flag_arraybounds) {
  102. ptr_arrmax = builder->
  103. CreateGEP(ptr_arr, ConstantInt::get(C, APInt(32, memtotal)), "arrmax");
  104. }
  105. //%head.%d = getelementptr i8 *%arr, i32 %d
  106. curhead = builder->CreateGEP(ptr_arr,
  107. ConstantInt::get(C, APInt(32, memtotal/2)),
  108. headreg);
  109. //Function footer
  110. //brainf.end:
  111. endbb = BasicBlock::Create(C, label, brainf_func);
  112. //call free(i8 *%arr)
  113. endbb->getInstList().push_back(CallInst::CreateFree(ptr_arr, endbb));
  114. //ret void
  115. ReturnInst::Create(C, endbb);
  116. //Error block for array out of bounds
  117. if (comflag & flag_arraybounds)
  118. {
  119. //@aberrormsg = internal constant [%d x i8] c"\00"
  120. Constant *msg_0 =
  121. ConstantDataArray::getString(C, "Error: The head has left the tape.",
  122. true);
  123. GlobalVariable *aberrormsg = new GlobalVariable(
  124. *module,
  125. msg_0->getType(),
  126. true,
  127. GlobalValue::InternalLinkage,
  128. msg_0,
  129. "aberrormsg");
  130. //declare i32 @puts(i8 *)
  131. Function *puts_func = cast<Function>(module->
  132. getOrInsertFunction("puts", IntegerType::getInt32Ty(C),
  133. PointerType::getUnqual(IntegerType::getInt8Ty(C))));
  134. //brainf.aberror:
  135. aberrorbb = BasicBlock::Create(C, label, brainf_func);
  136. //call i32 @puts(i8 *getelementptr([%d x i8] *@aberrormsg, i32 0, i32 0))
  137. {
  138. Constant *zero_32 = Constant::getNullValue(IntegerType::getInt32Ty(C));
  139. Constant *gep_params[] = {
  140. zero_32,
  141. zero_32
  142. };
  143. Constant *msgptr = ConstantExpr::
  144. getGetElementPtr(aberrormsg->getValueType(), aberrormsg, gep_params);
  145. Value *puts_params[] = {
  146. msgptr
  147. };
  148. CallInst *puts_call =
  149. CallInst::Create(puts_func,
  150. puts_params,
  151. "", aberrorbb);
  152. puts_call->setTailCall(false);
  153. }
  154. //br label %brainf.end
  155. BranchInst::Create(endbb, aberrorbb);
  156. }
  157. }
  158. void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb,
  159. LLVMContext &C) {
  160. Symbol cursym = SYM_NONE;
  161. int curvalue = 0;
  162. Symbol nextsym = SYM_NONE;
  163. int nextvalue = 0;
  164. char c;
  165. int loop;
  166. int direction;
  167. while(cursym != SYM_EOF && cursym != SYM_ENDLOOP) {
  168. // Write out commands
  169. switch(cursym) {
  170. case SYM_NONE:
  171. // Do nothing
  172. break;
  173. case SYM_READ:
  174. {
  175. //%tape.%d = call i32 @getchar()
  176. CallInst *getchar_call =
  177. builder->CreateCall(getchar_func, {}, tapereg);
  178. getchar_call->setTailCall(false);
  179. Value *tape_0 = getchar_call;
  180. //%tape.%d = trunc i32 %tape.%d to i8
  181. Value *tape_1 = builder->
  182. CreateTrunc(tape_0, IntegerType::getInt8Ty(C), tapereg);
  183. //store i8 %tape.%d, i8 *%head.%d
  184. builder->CreateStore(tape_1, curhead);
  185. }
  186. break;
  187. case SYM_WRITE:
  188. {
  189. //%tape.%d = load i8 *%head.%d
  190. LoadInst *tape_0 = builder->CreateLoad(curhead, tapereg);
  191. //%tape.%d = sext i8 %tape.%d to i32
  192. Value *tape_1 = builder->
  193. CreateSExt(tape_0, IntegerType::getInt32Ty(C), tapereg);
  194. //call i32 @putchar(i32 %tape.%d)
  195. Value *putchar_params[] = {
  196. tape_1
  197. };
  198. CallInst *putchar_call = builder->
  199. CreateCall(putchar_func,
  200. putchar_params);
  201. putchar_call->setTailCall(false);
  202. }
  203. break;
  204. case SYM_MOVE:
  205. {
  206. //%head.%d = getelementptr i8 *%head.%d, i32 %d
  207. curhead = builder->
  208. CreateGEP(curhead, ConstantInt::get(C, APInt(32, curvalue)),
  209. headreg);
  210. //Error block for array out of bounds
  211. if (comflag & flag_arraybounds)
  212. {
  213. //%test.%d = icmp uge i8 *%head.%d, %arrmax
  214. Value *test_0 = builder->
  215. CreateICmpUGE(curhead, ptr_arrmax, testreg);
  216. //%test.%d = icmp ult i8 *%head.%d, %arr
  217. Value *test_1 = builder->
  218. CreateICmpULT(curhead, ptr_arr, testreg);
  219. //%test.%d = or i1 %test.%d, %test.%d
  220. Value *test_2 = builder->
  221. CreateOr(test_0, test_1, testreg);
  222. //br i1 %test.%d, label %main.%d, label %main.%d
  223. BasicBlock *nextbb = BasicBlock::Create(C, label, brainf_func);
  224. builder->CreateCondBr(test_2, aberrorbb, nextbb);
  225. //main.%d:
  226. builder->SetInsertPoint(nextbb);
  227. }
  228. }
  229. break;
  230. case SYM_CHANGE:
  231. {
  232. //%tape.%d = load i8 *%head.%d
  233. LoadInst *tape_0 = builder->CreateLoad(curhead, tapereg);
  234. //%tape.%d = add i8 %tape.%d, %d
  235. Value *tape_1 = builder->
  236. CreateAdd(tape_0, ConstantInt::get(C, APInt(8, curvalue)), tapereg);
  237. //store i8 %tape.%d, i8 *%head.%d\n"
  238. builder->CreateStore(tape_1, curhead);
  239. }
  240. break;
  241. case SYM_LOOP:
  242. {
  243. //br label %main.%d
  244. BasicBlock *testbb = BasicBlock::Create(C, label, brainf_func);
  245. builder->CreateBr(testbb);
  246. //main.%d:
  247. BasicBlock *bb_0 = builder->GetInsertBlock();
  248. BasicBlock *bb_1 = BasicBlock::Create(C, label, brainf_func);
  249. builder->SetInsertPoint(bb_1);
  250. // Make part of PHI instruction now, wait until end of loop to finish
  251. PHINode *phi_0 =
  252. PHINode::Create(PointerType::getUnqual(IntegerType::getInt8Ty(C)),
  253. 2, headreg, testbb);
  254. phi_0->addIncoming(curhead, bb_0);
  255. curhead = phi_0;
  256. readloop(phi_0, bb_1, testbb, C);
  257. }
  258. break;
  259. default:
  260. std::cerr << "Error: Unknown symbol.\n";
  261. abort();
  262. break;
  263. }
  264. cursym = nextsym;
  265. curvalue = nextvalue;
  266. nextsym = SYM_NONE;
  267. // Reading stdin loop
  268. loop = (cursym == SYM_NONE)
  269. || (cursym == SYM_MOVE)
  270. || (cursym == SYM_CHANGE);
  271. while(loop) {
  272. *in>>c;
  273. if (in->eof()) {
  274. if (cursym == SYM_NONE) {
  275. cursym = SYM_EOF;
  276. } else {
  277. nextsym = SYM_EOF;
  278. }
  279. loop = 0;
  280. } else {
  281. direction = 1;
  282. switch(c) {
  283. case '-':
  284. direction = -1;
  285. LLVM_FALLTHROUGH;
  286. case '+':
  287. if (cursym == SYM_CHANGE) {
  288. curvalue += direction;
  289. // loop = 1
  290. } else {
  291. if (cursym == SYM_NONE) {
  292. cursym = SYM_CHANGE;
  293. curvalue = direction;
  294. // loop = 1
  295. } else {
  296. nextsym = SYM_CHANGE;
  297. nextvalue = direction;
  298. loop = 0;
  299. }
  300. }
  301. break;
  302. case '<':
  303. direction = -1;
  304. LLVM_FALLTHROUGH;
  305. case '>':
  306. if (cursym == SYM_MOVE) {
  307. curvalue += direction;
  308. // loop = 1
  309. } else {
  310. if (cursym == SYM_NONE) {
  311. cursym = SYM_MOVE;
  312. curvalue = direction;
  313. // loop = 1
  314. } else {
  315. nextsym = SYM_MOVE;
  316. nextvalue = direction;
  317. loop = 0;
  318. }
  319. }
  320. break;
  321. case ',':
  322. if (cursym == SYM_NONE) {
  323. cursym = SYM_READ;
  324. } else {
  325. nextsym = SYM_READ;
  326. }
  327. loop = 0;
  328. break;
  329. case '.':
  330. if (cursym == SYM_NONE) {
  331. cursym = SYM_WRITE;
  332. } else {
  333. nextsym = SYM_WRITE;
  334. }
  335. loop = 0;
  336. break;
  337. case '[':
  338. if (cursym == SYM_NONE) {
  339. cursym = SYM_LOOP;
  340. } else {
  341. nextsym = SYM_LOOP;
  342. }
  343. loop = 0;
  344. break;
  345. case ']':
  346. if (cursym == SYM_NONE) {
  347. cursym = SYM_ENDLOOP;
  348. } else {
  349. nextsym = SYM_ENDLOOP;
  350. }
  351. loop = 0;
  352. break;
  353. // Ignore other characters
  354. default:
  355. break;
  356. }
  357. }
  358. }
  359. }
  360. if (cursym == SYM_ENDLOOP) {
  361. if (!phi) {
  362. std::cerr << "Error: Extra ']'\n";
  363. abort();
  364. }
  365. // Write loop test
  366. {
  367. //br label %main.%d
  368. builder->CreateBr(testbb);
  369. //main.%d:
  370. //%head.%d = phi i8 *[%head.%d, %main.%d], [%head.%d, %main.%d]
  371. //Finish phi made at beginning of loop
  372. phi->addIncoming(curhead, builder->GetInsertBlock());
  373. Value *head_0 = phi;
  374. //%tape.%d = load i8 *%head.%d
  375. LoadInst *tape_0 = new LoadInst(head_0, tapereg, testbb);
  376. //%test.%d = icmp eq i8 %tape.%d, 0
  377. ICmpInst *test_0 = new ICmpInst(*testbb, ICmpInst::ICMP_EQ, tape_0,
  378. ConstantInt::get(C, APInt(8, 0)), testreg);
  379. //br i1 %test.%d, label %main.%d, label %main.%d
  380. BasicBlock *bb_0 = BasicBlock::Create(C, label, brainf_func);
  381. BranchInst::Create(bb_0, oldbb, test_0, testbb);
  382. //main.%d:
  383. builder->SetInsertPoint(bb_0);
  384. //%head.%d = phi i8 *[%head.%d, %main.%d]
  385. PHINode *phi_1 = builder->
  386. CreatePHI(PointerType::getUnqual(IntegerType::getInt8Ty(C)), 1,
  387. headreg);
  388. phi_1->addIncoming(head_0, testbb);
  389. curhead = phi_1;
  390. }
  391. return;
  392. }
  393. //End of the program, so go to return block
  394. builder->CreateBr(endbb);
  395. if (phi) {
  396. std::cerr << "Error: Missing ']'\n";
  397. abort();
  398. }
  399. }