Pegmatite
 All Classes Functions Variables Enumerations
main.cc
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 #include <iostream>
22 #include <ctype.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/resource.h>
27 #include <time.h>
28 #include <unistd.h>
29 #include "parser.hh"
30 #include "ast.hh"
31 
32 static int enableTiming = 0;
33 
34 static void logTimeSince(clock_t c1, const char *msg)
35 {
36  if (!enableTiming) { return; }
37  clock_t c2 = clock();
38  struct rusage r;
39  getrusage(RUSAGE_SELF, &r);
40  fprintf(stderr, "%s took %f seconds. Peak used %ldKB.\n", msg,
41  ((double)c2 - (double)c1) / (double)CLOCKS_PER_SEC, r.ru_maxrss);
42 }
43 
44 int main(int argc, char **argv)
45 {
46  int iterations = 1;
47  int useJIT = 0;
48  int optimiseLevel = 0;
49  int gridSize = 5;
50  int maxValue = 1;
51  clock_t c1;
52  int c;
53  while ((c = getopt(argc, argv, "ji:tO:x:m:")) != -1)
54  {
55  switch (c)
56  {
57  case 'j':
58  useJIT = 1;
59  break;
60  case 'x':
61  gridSize = strtol(optarg, 0, 10);
62  break;
63  case 'm':
64  maxValue = strtol(optarg, 0, 10);
65  break;
66  case 'i':
67  iterations = strtol(optarg, 0, 10);
68  break;
69  case 't':
70  enableTiming = 1;
71  break;
72  case 'o':
73  optimiseLevel = strtol(optarg, 0, 10);
74  }
75  }
76  argc -= optind;
77  if (argc < 1)
78  {
79  fprintf(stderr, "usage: %s -jt -i {iterations} -o {optimisation level} -x {grid size} -m {max initial value} {file name}\n", argv[0]);
80  return EXIT_FAILURE;
81  }
82  argv += optind;
83 
84  // Do the parsing
86  pegmatite::AsciiFileInput input(open(argv[0], O_RDONLY));
87  pegmatite::ErrorList el;
88  std::unique_ptr<AST::StatementList> ast = 0;
89  c1 = clock();
90  if (!p.parse(input, p.g.statements, p.g.ignored, el, ast))
91  {
92  std::cout << "errors: \n";
93  for (auto &err : el)
94  {
95  std::cout << "line " << err.start.line
96  << ", col " << err.finish.col << ": ";
97  std::cout << "syntax error" << std::endl;
98  }
99  return EXIT_FAILURE;
100  }
101  logTimeSince(c1, "Parsing program");
102  assert(ast);
103 
104 
105 #ifdef DUMP_AST
106  for (uintptr_t i=0 ; i<result->count ; i++)
107  {
108  printAST(result->list[i]);
109  putchar('\n');
110  }
111 #endif
112 #ifdef STATIC_TESTING_GRID
113  int16_t oldgrid[] = {
114  0,0,0,0,0,
115  0,0,0,0,0,
116  0,1,1,1,0,
117  0,0,0,0,0,
118  0,0,0,0,0
119  };
120  int16_t newgrid[25];
121  gridSize = 5;
122  int16_t *g1 = oldgrid;
123  int16_t *g2 = newgrid;
124 #else
125  int16_t *g1 = (int16_t*)malloc(sizeof(int16_t) * gridSize * gridSize);
126  int16_t *g2 = (int16_t*)malloc(sizeof(int16_t) * gridSize * gridSize);
127  //int16_t *g2 = new int16_t[gridSize * gridSize];
128  c1 = clock();
129  for (int i=0 ; i<(gridSize*gridSize) ; i++)
130  {
131  g1[i] = random() % (maxValue + 1);
132  }
133  logTimeSince(c1, "Generating random grid");
134 #endif
135  int i=0;
136  if (useJIT)
137  {
138  c1 = clock();
139  Compiler::automaton ca = Compiler::compile(ast.get(), optimiseLevel);
140  logTimeSince(c1, "Compiling");
141  c1 = clock();
142  for (int i=0 ; i<iterations ; i++)
143  {
144  ca(g1, g2, gridSize, gridSize);
145  std::swap(g1, g2);
146  }
147  logTimeSince(c1, "Running compiled version");
148  }
149  else
150  {
151  c1 = clock();
152  for (int i=0 ; i<iterations ; i++)
153  {
154  Interpreter::runOneStep(g1, g2, gridSize, gridSize, ast.get());
155  std::swap(g1, g2);
156  }
157  logTimeSince(c1, "Interpreting");
158  }
159  for (int x=0 ; x<gridSize ; x++)
160  {
161  for (int y=0 ; y<gridSize ; y++)
162  {
163  printf("%d ", g1[i++]);
164  }
165  putchar('\n');
166  }
167  return 0;
168 }
Rule statements
List of zero or more statements.
Definition: parser.hh:118
Class representing a parser for the CellAtom language.
Definition: parser.hh:139
Rule ignored
Rule for treating both comments and whitespace as ignored tokens.
Definition: parser.hh:51