comparison examples/BrainF/BrainF.cpp @ 120:1172e4bd9c6f

update 4.0.0
author mir3636
date Fri, 25 Nov 2016 19:14:25 +0900
parents afa8332a0e37
children 803732b1fca8
comparison
equal deleted inserted replaced
101:34baf5011add 120:1172e4bd9c6f
1 //===-- BrainF.cpp - BrainF compiler example ----------------------------===// 1 //===-- BrainF.cpp - BrainF compiler example ------------------------------===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // The LLVM Compiler Infrastructure
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===--------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 // 9 //
10 // This class compiles the BrainF language into LLVM assembly. 10 // This class compiles the BrainF language into LLVM assembly.
11 // 11 //
12 // The BrainF language has 8 commands: 12 // The BrainF language has 8 commands:
13 // Command Equivalent C Action 13 // Command Equivalent C Action
19 // < --h; Move head left 19 // < --h; Move head left
20 // > ++h; Move head right 20 // > ++h; Move head right
21 // [ while(*h) { Start loop 21 // [ while(*h) { Start loop
22 // ] } End loop 22 // ] } End loop
23 // 23 //
24 //===--------------------------------------------------------------------===// 24 //===----------------------------------------------------------------------===//
25 25
26 #include "BrainF.h" 26 #include "BrainF.h"
27 #include "llvm/ADT/STLExtras.h" 27 #include "llvm/ADT/APInt.h"
28 #include "llvm/IR/BasicBlock.h"
29 #include "llvm/IR/Constant.h"
28 #include "llvm/IR/Constants.h" 30 #include "llvm/IR/Constants.h"
31 #include "llvm/IR/DerivedTypes.h"
32 #include "llvm/IR/Function.h"
33 #include "llvm/IR/GlobalValue.h"
34 #include "llvm/IR/GlobalVariable.h"
35 #include "llvm/IR/InstrTypes.h"
36 #include "llvm/IR/Instruction.h"
29 #include "llvm/IR/Instructions.h" 37 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/Intrinsics.h" 38 #include "llvm/IR/Intrinsics.h"
39 #include "llvm/IR/Module.h"
40 #include "llvm/IR/Type.h"
41 #include "llvm/Support/Casting.h"
42 #include <cstdlib>
31 #include <iostream> 43 #include <iostream>
32 44
33 using namespace llvm; 45 using namespace llvm;
34 46
35 //Set the constants for naming 47 //Set the constants for naming
325 } else { 337 } else {
326 direction = 1; 338 direction = 1;
327 switch(c) { 339 switch(c) {
328 case '-': 340 case '-':
329 direction = -1; 341 direction = -1;
330 // Fall through 342 LLVM_FALLTHROUGH;
331 343
332 case '+': 344 case '+':
333 if (cursym == SYM_CHANGE) { 345 if (cursym == SYM_CHANGE) {
334 curvalue += direction; 346 curvalue += direction;
335 // loop = 1 347 // loop = 1
346 } 358 }
347 break; 359 break;
348 360
349 case '<': 361 case '<':
350 direction = -1; 362 direction = -1;
351 // Fall through 363 LLVM_FALLTHROUGH;
352 364
353 case '>': 365 case '>':
354 if (cursym == SYM_MOVE) { 366 if (cursym == SYM_MOVE) {
355 curvalue += direction; 367 curvalue += direction;
356 // loop = 1 368 // loop = 1