BrainF.cpp 13 KB

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