Pegmatite
 All Classes Functions Variables Enumerations
ast.hh
1 /*
2  * Copyright (c) 2014 David Chisnall
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy of
5  * this software and associated documentation files (the "Software"), to deal in
6  * the Software without restriction, including without limitation the rights to
7  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8  * the Software, and to permit persons to whom the Software is furnished to do so,
9  * subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20  */
21 #ifndef CELLATOM_AST_H_INCLUDED
22 #define CELLATOM_AST_H_INCLUDED
23 #include <stdint.h>
24 #include "Pegmatite/pegmatite.hh"
25 
26 namespace AST
27 {
28  struct StatementList;
29 }
30 
31 namespace Interpreter
32 {
36  struct State;
40  void runOneStep(int16_t *oldgrid,
41  int16_t *newgrid,
42  int16_t width,
43  int16_t height,
44  AST::StatementList *ast);
45 }
46 
47 namespace Compiler
48 {
52  struct State;
57  typedef void(*automaton)(int16_t *oldgrid,
58  int16_t *newgrid,
59  int16_t width,
60  int16_t height);
65  automaton compile(AST::StatementList *ast, int optimiseLevel);
66 }
67 namespace llvm
68 {
69  class Value;
70 }
71 
72 namespace AST
73 {
77  struct Statement : public pegmatite::ASTContainer
78  {
83  virtual uint16_t interpret(Interpreter::State &) = 0;
88  virtual llvm::Value *compile(Compiler::State &) = 0;
89  };
90 
95  {
99  pegmatite::ASTList<Statement> statements;
100  uint16_t interpret(Interpreter::State&) override;
101  virtual llvm::Value *compile(Compiler::State &) override;
102  };
103 
107  struct Literal : public Statement
108  {
112  uint16_t value;
113  virtual uint16_t interpret(Interpreter::State &) override;
114  virtual void construct(const pegmatite::InputRange &r,
115  pegmatite::ASTStack &st) override;
116  virtual llvm::Value *compile(Compiler::State &) override;
117  };
118 
122  struct Register : public Statement
123  {
127  virtual uint16_t interpret(Interpreter::State &) = 0;
131  virtual void assign(Interpreter::State &, uint16_t) = 0;
136  virtual llvm::Value *compile(Compiler::State &) = 0;
141  virtual void assign(Compiler::State &, llvm::Value*) = 0;
142  };
143 
147  struct VRegister : public Register
148  {
149  virtual uint16_t interpret(Interpreter::State &) override;
150  virtual void assign(Interpreter::State &, uint16_t) override;
151  virtual llvm::Value *compile(Compiler::State &) override;
152  virtual void assign(Compiler::State &, llvm::Value*) override;
153  };
154 
159  struct LocalRegister : public Register
160  {
165  virtual void construct(const pegmatite::InputRange &r,
166  pegmatite::ASTStack &st) override;
167  virtual uint16_t interpret(Interpreter::State &) override;
168  virtual void assign(Interpreter::State &, uint16_t) override;
169  virtual llvm::Value *compile(Compiler::State &) override;
170  virtual void assign(Compiler::State &, llvm::Value*) override;
171  };
172 
177  struct GlobalRegister : public Register
178  {
183  virtual void construct(const pegmatite::InputRange &r,
184  pegmatite::ASTStack &st) override;
185  virtual uint16_t interpret(Interpreter::State &) override;
186  virtual void assign(Interpreter::State &, uint16_t) override;
187  virtual llvm::Value *compile(Compiler::State &) override;
188  virtual void assign(Compiler::State &, llvm::Value*) override;
189  };
194  struct Op: pegmatite::ASTNode
195  {
199  enum OpKind
200  {
201  Add,
202  Assign,
203  Sub,
204  Mul,
205  Div,
206  Min,
207  Max
208  } op;
209  virtual void construct(const pegmatite::InputRange &r,
210  pegmatite::ASTStack &st) override;
211  };
216  struct Arithmetic : public Statement
217  {
221  pegmatite::ASTPtr<Op> op;
226  pegmatite::ASTPtr<Register> target;
230  pegmatite::ASTPtr<Statement> value;
231  virtual uint16_t interpret(Interpreter::State &) override;
232  virtual llvm::Value *compile(Compiler::State &) override;
233  };
234 
239  struct Range : public pegmatite::ASTContainer
240  {
244  pegmatite::ASTPtr<Literal, /* optional */true> start;
249  pegmatite::ASTPtr<Literal> end;
253  pegmatite::ASTPtr<Statement> value;
254  };
255 
260  struct RangeExpr : public Statement
261  {
265  pegmatite::ASTPtr<Register> value;
269  pegmatite::ASTList<Range> ranges;
270  virtual uint16_t interpret(Interpreter::State &) override;
271  virtual llvm::Value *compile(Compiler::State &) override;
272  };
273 
278  {
282  pegmatite::ASTPtr<StatementList> statements;
283  virtual uint16_t interpret(Interpreter::State &) override;
284  virtual llvm::Value *compile(Compiler::State &) override;
285  };
286 
287 
288 }
289 
290 
291 #endif // CELLATOM_AST_H_INCLUDED
AST node for a neighbours expression.
Definition: ast.hh:277
virtual uint16_t interpret(Interpreter::State &) override
Interpret this node, updating the given interpreter step and returning the value (if there is one)...
Definition: interpreter.cc:91
pegmatite::ASTPtr< Statement > value
The value to assign to the register.
Definition: ast.hh:230
Definition: ast.hh:67
pegmatite::ASTPtr< Op > op
The operation to perform.
Definition: ast.hh:221
pegmatite::ASTPtr< Statement > value
The value to use when if this range is matched.
Definition: ast.hh:253
virtual uint16_t interpret(Interpreter::State &)=0
Returns the value in this register (when interpreting the AST).
virtual llvm::Value * compile(Compiler::State &)=0
Compile this node, returning the LLVM value representing the result, if there is one.
Abstract base class for all register types in the AST.
Definition: ast.hh:122
A range within a range expression.
Definition: ast.hh:239
uint16_t value
The value for this literal.
Definition: ast.hh:112
virtual uint16_t interpret(Interpreter::State &) override
Interpret this node, updating the given interpreter step and returning the value (if there is one)...
Definition: interpreter.cc:146
virtual uint16_t interpret(Interpreter::State &) override
Returns the value in this register (when interpreting the AST).
Definition: interpreter.cc:64
Arithmetic nodes, for example '+ a0 12' (add the value 12 to register a0).
Definition: ast.hh:216
virtual void assign(Interpreter::State &, uint16_t)=0
Assigns a value to the register (when interpreting the AST).
virtual llvm::Value * compile(Compiler::State &) override
Returns an LLVM value representing an access to this register (when compiling).
Definition: compiler.cc:269
A list of statements that will be executed sequentially.
Definition: ast.hh:94
A range expression, for example [ a0 | (2,3) => 1] (if the value of register a0 is 2-3 inclusive...
Definition: ast.hh:260
Value representing the operation to use in an arithmetic / assignment statement.
Definition: ast.hh:194
pegmatite::ASTPtr< Register > value
The register that is being compared against one or more ranges.
Definition: ast.hh:265
virtual llvm::Value * compile(Compiler::State &) override
Returns an LLVM value representing an access to this register (when compiling).
Definition: compiler.cc:249
virtual llvm::Value * compile(Compiler::State &) override
Compile this node, returning the LLVM value representing the result, if there is one.
Definition: compiler.cc:321
virtual uint16_t interpret(Interpreter::State &)=0
Interpret this node, updating the given interpreter step and returning the value (if there is one)...
The current state for the interpreter.
Definition: interpreter.cc:11
Definition: ast.hh:47
pegmatite::ASTPtr< Literal, true > start
The start value for expressions of the form (start, end) =>
Definition: ast.hh:244
virtual llvm::Value * compile(Compiler::State &) override
Compile this node, returning the LLVM value representing the result, if there is one.
Definition: compiler.cc:278
pegmatite::ASTList< Range > ranges
The ranges in this range map.
Definition: ast.hh:269
A (number) literal.
Definition: ast.hh:107
OpKind
The type of operation to perform.
Definition: ast.hh:199
virtual uint16_t interpret(Interpreter::State &) override
Interpret this node, updating the given interpreter step and returning the value (if there is one)...
Definition: interpreter.cc:60
The v register, which represents the current value in the grid.
Definition: ast.hh:147
virtual llvm::Value * compile(Compiler::State &) override
Returns an LLVM value representing an access to this register (when compiling).
Definition: compiler.cc:259
int registerNumber
The number of the global register that this references.
Definition: ast.hh:182
pegmatite::ASTPtr< Register > target
The target register.
Definition: ast.hh:226
virtual llvm::Value * compile(Compiler::State &) override
Compile this node, returning the LLVM value representing the result, if there is one.
Definition: compiler.cc:245
pegmatite::ASTList< Statement > statements
The ordered list of statements.
Definition: ast.hh:99
pegmatite::ASTPtr< StatementList > statements
The statements contained within this neighbours block.
Definition: ast.hh:282
Definition: ast.hh:26
virtual void assign(Interpreter::State &, uint16_t) override
Assigns a value to the register (when interpreting the AST).
Definition: interpreter.cc:69
int registerNumber
The number of the local register that this references.
Definition: ast.hh:164
A reference to one of the ten local registers, whose values are initially zero on entry into the cell...
Definition: ast.hh:159
Root class for all statement AST nodes in the Cellular Automata grammar.
Definition: ast.hh:77
virtual uint16_t interpret(Interpreter::State &) override
Interpret this node, updating the given interpreter step and returning the value (if there is one)...
Definition: interpreter.cc:124
pegmatite::ASTPtr< Literal > end
The end value for values that have a start and end, or the single value where only one is given...
Definition: ast.hh:249
virtual uint16_t interpret(Interpreter::State &) override
Returns the value in this register (when interpreting the AST).
Definition: interpreter.cc:74
uint16_t interpret(Interpreter::State &) override
Interpret this node, updating the given interpreter step and returning the value (if there is one)...
Definition: interpreter.cc:165
A reference to one of the ten global registers, whose values are only reset after updating an entire ...
Definition: ast.hh:177
virtual void assign(Interpreter::State &, uint16_t) override
Assigns a value to the register (when interpreting the AST).
Definition: interpreter.cc:78
virtual llvm::Value * compile(Compiler::State &) override
Compile this node, returning the LLVM value representing the result, if there is one.
Definition: compiler.cc:390
virtual llvm::Value * compile(Compiler::State &) override
Compile this node, returning the LLVM value representing the result, if there is one.
Definition: compiler.cc:478
virtual uint16_t interpret(Interpreter::State &) override
Returns the value in this register (when interpreting the AST).
Definition: interpreter.cc:82
virtual void assign(Interpreter::State &, uint16_t) override
Assigns a value to the register (when interpreting the AST).
Definition: interpreter.cc:86
virtual llvm::Value * compile(Compiler::State &)=0
Returns an LLVM value representing an access to this register (when compiling).