summaryrefslogtreecommitdiff
path: root/examples/BFtoLLVM/BFtoLLVM.cpp
blob: a85ef24537d10b0d3f74ee95eb0edbbfae2e8d78 (plain)
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
//===-- BFtoLLVM.cpp - BF language Front End for LLVM ---------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This is a simple front end for the BF language.  It is compatible with the
// language as described in "The BrainF*** Language Specification (01 January
// 2002)", which is available from http://esoteric.sange.fi/ENSI . It does not
// implement the optional keyword # ("Output partial tape state").
//
//===----------------------------------------------------------------------===//

#include <iostream>
#include <vector>
#include <fstream>
#include <cerrno>
#include <cstring>
#include <string>
#include <cstdio>
#include <cassert>

void emitDeclarations(std::ofstream &dest) {
  dest << "; This assembly code brought to you by BFtoLLVM\n"
       << "\nimplementation\n"
       << "\n; Declarations\n"
       << "\ndeclare int %getchar()\n"
       << "declare int %putchar(int)\n"
       << "declare void %llvm.memset.i32(sbyte*, ubyte, uint, uint)\n"
       << "\n";
}

void emitMainFunctionProlog(std::ofstream &dest) {
  dest << "\n; Main function\n"
       << "int %main(int %argc, sbyte** %argv) {\n"
       << "\nentry:\n"
       << "%arr = alloca sbyte, uint 30000\n"
       << "call void (sbyte*, ubyte, uint, uint)* %llvm.memset.i32"
       << "(sbyte* %arr, ubyte 0, uint 30000, uint 1)\n"
       << "%ptrbox = alloca sbyte*\n"
       << "store sbyte* %arr, sbyte **%ptrbox\n"
       << "\n";
}

void emitMainFunctionEpilog(std::ofstream &dest) {
  dest << "ret int 0\n"
       << "}\n";
}

std::string gensym (const std::string varName, bool percent = true) {
  char buf[80];
  static unsigned int SymbolCounter = 0;
  sprintf (buf, "%s%s%u", percent ? "%" : "", varName.c_str(), SymbolCounter++);
  return std::string (buf);
}

void emitArith (std::string op, char delta, std::ofstream &dest) {
  std::string ptr = gensym (op + "ptr"),
              val = gensym (op + "val"),
              result = gensym (op + "result");
  dest << ptr << " = load sbyte** %ptrbox\n"
       << val << " = load sbyte* " << ptr << "\n"
       << result << " = add sbyte " << val << ", " << (int)delta << "\n"
       << "store sbyte " << result << ", sbyte* " << ptr << "\n";
}

// + becomes ++*p; and - becomes --*p;
void emitPlus  (std::ofstream &dest, int ct) { emitArith ("plus",  +ct, dest); }
void emitMinus (std::ofstream &dest, int ct) { emitArith ("minus", -ct, dest); }

void emitLoadAndCast (std::string ptr, std::string val, std::string cast,
                      std::string type, std::ofstream &dest) {
  dest << ptr << " = load sbyte** %ptrbox\n"
       << val << " = load sbyte* " << ptr << "\n"
       << cast << " = cast sbyte " << val << " to " << type << "\n";
}

// , becomes *p = getchar();
void emitComma(std::ofstream &dest, int ct) {
  assert (ct == 1);
  std::string ptr = gensym("commaptr"), read = gensym("commaread"),
              cast = gensym("commacast");
  dest << ptr << " = load sbyte** %ptrbox\n"
       << read << " = call int %getchar()\n"
       << cast << " = cast int " << read << " to sbyte\n"
       << "store sbyte " << cast << ", sbyte* " << ptr << "\n";
}

// . becomes putchar(*p);
void emitDot(std::ofstream &dest, int ct) {
  assert (ct == 1);
  std::string ptr = gensym("dotptr"), val = gensym("dotval"),
              cast = gensym("dotcast");
  emitLoadAndCast (ptr, val, cast, "int", dest);
  dest << "call int %putchar(int " << cast << ")\n";
}

void emitPointerArith(std::string opname, int delta, std::ofstream &dest) {
  std::string ptr = gensym(opname + "ptr"), result = gensym(opname + "result");
  dest << ptr << " = load sbyte** %ptrbox\n"
       << result << " = getelementptr sbyte* " << ptr << ", int " << delta
       << "\n"
       << "store sbyte* " << result << ", sbyte** %ptrbox\n";
}

// < becomes --p; and > becomes ++p;
void emitLT(std::ofstream &dest, int ct) { emitPointerArith ("lt", -ct, dest); }
void emitGT(std::ofstream &dest, int ct) { emitPointerArith ("gt", +ct, dest); }

static std::vector<std::string> whileStack;

// [ becomes while (*p) {
void emitLeftBracket(std::ofstream &dest, int ct) {
  assert (ct == 1);
  std::string whileName = gensym ("While", false);
  whileStack.push_back (whileName);
  dest << "br label %testFor" << whileName << "\n"
       << "\ninside" << whileName << ":\n";
}

// ] becomes }
void emitRightBracket(std::ofstream &dest, int ct) {
  assert (ct == 1);
  std::string whileName = whileStack.back (),
              ptr = gensym("bracketptr"),
              val = gensym("bracketval"),
              cast = gensym("bracketcast");
  whileStack.pop_back ();
  dest << "br label %testFor" << whileName << "\n"
       << "\ntestFor" << whileName << ":\n";
  emitLoadAndCast (ptr, val, cast, "bool", dest);
  dest << "br bool " << cast << ", label %inside" << whileName << ", "
       << "label %after" << whileName << "\n"
       << "\nafter" << whileName << ":\n";
}

typedef void (*FuncTy)(std::ofstream &, int);
static FuncTy table[256];
static bool multi[256];

void consume (int ch, int repeatCount, std::ofstream &dest) {
  FuncTy func = table[ch];
  if (!func)
    return;
  else if (multi[ch])
    func (dest, repeatCount);
  else
    for (int i = 0; i < repeatCount; ++i)
      func (dest, 1);
}

void initializeTable() {
  memset (table, 0, 256);
  memset (multi, 0, 256);
  table[(int)'+'] = emitPlus;          multi[(int)'+'] = true;
  table[(int)'-'] = emitMinus;         multi[(int)'-'] = true;
  table[(int)','] = emitComma;         multi[(int)','] = false;
  table[(int)'.'] = emitDot;           multi[(int)'.'] = false;
  table[(int)'<'] = emitLT;            multi[(int)'<'] = true;
  table[(int)'>'] = emitGT;            multi[(int)'>'] = true;
  table[(int)'['] = emitLeftBracket;   multi[(int)'['] = false;
  table[(int)']'] = emitRightBracket;  multi[(int)']'] = false;
}

int main (int argc, char **argv) {
  if (argc != 3) {
    std::cerr << "usage: " << argv[0] << " input-source output-llvm\n";
    return 1;
  }

  char *sourceFileName = argv[1];
  char *destFileName = argv[2];

  std::ifstream src (sourceFileName);
  if (!src.good()) {
    std::cerr << sourceFileName << ": " << strerror(errno) << "\n";
    return 1;
  }

  std::ofstream dest (destFileName);
  if (!dest.good()) {
    std::cerr << destFileName << ": " << strerror(errno) << "\n";
    return 1;
  }

  emitDeclarations(dest);
  emitMainFunctionProlog(dest);

  initializeTable();
  char ch, lastCh;
  src >> lastCh;
  int repeatCount = 1;
  for (src >> ch; !src.eof (); src >> ch, ++repeatCount)
    if (ch != lastCh) {
      consume (lastCh, repeatCount, dest);
      lastCh = ch;
      repeatCount = 0;
    }
  consume (lastCh, repeatCount, dest);

  emitMainFunctionEpilog(dest);

  src.close();
  dest.close();
  return 0;
}