aboutsummaryrefslogtreecommitdiff
path: root/gcc/python/py-runtime.tpl
blob: 75e4f119d51b0049ae768667fa01a9209ce5d046 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
[+ AutoGen5 template -*- Mode: C -*-
h c
+]

/* This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

/* 
   This file was generated via autogen @see py-runtime.{def/tpl}
     - To regenerate with new definitions $ autogen py-runtime.def
 */

[+ CASE (suffix) +]
[+ == h +]
#ifndef __GCC_PY_RUNTIME_H__
#define __GCC_PY_RUNTIME_H__

#define GPY_RR_globl_stack         "__GPY_GLOBL_RR_STACK"
#define GPY_RR_stack_size          "__GPY_GLOBL_RR_STACK_SIZE"
#define GPY_RR_stack_data_offset   "__GPY_GLOBL_RR_STACK_DATA_OFFSET"
#define GPY_RR_globl_stack_pointer "__GPY_GLOBL_RR_STACK_POINTER"
#define GPY_RR_globl_call_stack    "__GPY_GLOBL_CALL_STACK"
#define GPY_RR_globl_primitives    "__GPY_GLOBL_PRIMITIVES"
#define GPY_RR_globl_return_addr   "__GPY_GLOBL_RETURN_ADDR"
#define GPY_RR_entry               "__GPY_entry"

/* return a const string tree */
extern tree gpy_dot_type_const_string_tree (const char *);
  [+ FOR py-runtime +]
[+ (get "comment") +]
extern tree [+ (get "code") +] ([+ (get "proto") +]);
  [+ ENDFOR py-runtime +]
#endif //__GCC_PY_RUNTIME_H__
[+ == c +]
#include "config.h"
#include "system.h"
#include "ansidecl.h"
#include "coretypes.h"
#include "tm.h"
#include "opts.h"
#include "tree.h"
#include "tree-iterator.h"
#include "tree-pass.h"
#include "gimple.h"
#include "toplev.h"
#include "debug.h"
#include "options.h"
#include "flags.h"
#include "convert.h"
#include "diagnostic-core.h"
#include "langhooks.h"
#include "langhooks-def.h"
#include "target.h"
#include "cgraph.h"

#include <gmp.h>
#include <mpfr.h>

#include <gpython.h>
#include <py-il-dot.h>
#include <py-il-tree.h>
#include <py-runtime.h>
#include <py-vec.h>

VEC(tree,gc) * gpy_builtin_types_vec;
static tree gpy_build_py_object_type (void);
static tree gpy_build_py_vector_type (void);

  [+ FOR py-runtime +]
tree [+ (get "code") +] ([+ (get "arguments") +])
{
  tree fntype = build_function_type_list ([+ (get "fntype") +]);
  tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
			    get_identifier ("[+ (get "function_identifier") +]"),
			    fntype);
  tree restype = TREE_TYPE (fndecl);
  tree resdecl = build_decl (BUILTINS_LOCATION, RESULT_DECL, NULL_TREE,
			     restype);
  DECL_CONTEXT (resdecl) = fndecl;
  DECL_RESULT (fndecl) = resdecl;
  DECL_EXTERNAL (fndecl) = 1;
  TREE_PUBLIC (fndecl) = 1;

  return [+ (get "build_call") +] ([+ (get "build_call_args") +]);
}
  [+ ENDFOR py-runtime +]

static
tree gpy_build_py_vector_type (void)
{
  tree vec_struct_type = make_node (RECORD_TYPE);

  tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("vector"),
			   build_pointer_type (ptr_type_node));
  DECL_CONTEXT (field) = vec_struct_type;
  TYPE_FIELDS (vec_struct_type) = field;
  tree last_field = field;

  field = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("size"),
		      integer_type_node);
  DECL_CONTEXT (field) = vec_struct_type;
  DECL_CHAIN (last_field) = field;
  last_field = field;

  field = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("length"),
		      integer_type_node);
  DECL_CONTEXT (field) = vec_struct_type;
  DECL_CHAIN (last_field) = field;

  layout_type (vec_struct_type);
  tree ident = get_identifier ("gpy_vector_t");
  tree type_decl = build_decl (BUILTINS_LOCATION, TYPE_DECL, ident,
			       vec_struct_type);
  DECL_ARTIFICIAL (type_decl) = 1;
  TYPE_NAME (vec_struct_type) = ident;

  gpy_preserve_from_gc (type_decl);
  rest_of_decl_compilation (type_decl, 1, 0);

  return vec_struct_type;
}

static
tree gpy_build_py_attrib_type (void)
{
  tree attrib_struct_type = make_node (RECORD_TYPE);

  tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
			   get_identifier ("identifier"),
			   gpy_const_char_ptr);
  DECL_CONTEXT (field) = attrib_struct_type;
  TYPE_FIELDS (attrib_struct_type) = field;

  tree last_field = field;

  field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
		      get_identifier ("offset"),
		      unsigned_type_node);
  DECL_CONTEXT (field) = attrib_struct_type;
  DECL_CHAIN (last_field) = field;
  last_field = field;

  field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
		      get_identifier ("addr"),
		      gpy_object_type_ptr);
  DECL_CONTEXT (field) = attrib_struct_type;
  DECL_CHAIN (last_field) = field;

  layout_type (attrib_struct_type);
  tree ident = get_identifier ("gpy_object_attrib_t");
  tree type_decl = build_decl (BUILTINS_LOCATION, TYPE_DECL, ident,
			       attrib_struct_type);
  DECL_ARTIFICIAL (type_decl) = 1;
  TYPE_NAME (attrib_struct_type) = ident;

  gpy_preserve_from_gc (type_decl);
  rest_of_decl_compilation (type_decl, 1, 0);

  return attrib_struct_type;
}

static
tree gpy_build_py_object_type (void)
{
  tree object_state_struct_Type = make_node (RECORD_TYPE);
  
  tree name = get_identifier("identifier");
  tree field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
			  build_pointer_type(char_type_node));
  DECL_CONTEXT(field) = object_state_struct_Type;
  TYPE_FIELDS(object_state_struct_Type) = field;
  tree last_field = field;

  name = get_identifier("ref_count");
  field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name, integer_type_node);
  DECL_CONTEXT(field) = object_state_struct_Type;
  DECL_CHAIN(last_field) = field;
  last_field = field;

  name = get_identifier("state");
  field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
		     ptr_type_node);
  DECL_CONTEXT(field) = object_state_struct_Type;
  DECL_CHAIN(last_field) = field;
  last_field = field;
  
  name = get_identifier("definition");
  field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
		     ptr_type_node);
  DECL_CONTEXT(field) = object_state_struct_Type;
  DECL_CHAIN(last_field) = field;
  last_field = field;

  layout_type(object_state_struct_Type);

  // Give the struct a name for better debugging info.
  name = get_identifier ("gpy_object_state_t");
  tree object_state_type_decl = build_decl (BUILTINS_LOCATION, TYPE_DECL, name,
					    object_state_struct_Type);
  DECL_ARTIFICIAL(object_state_type_decl) = 1;
  TYPE_NAME(object_state_struct_Type) = object_state_type_decl;
  gpy_preserve_from_gc(object_state_type_decl);
  rest_of_decl_compilation(object_state_type_decl, 1, 0);

  tree object_state_ptr_type = build_pointer_type (object_state_struct_Type);
  gpy_preserve_from_gc (object_state_ptr_type);

  tree union_type__ = make_node (UNION_TYPE);

  name = get_identifier ("object_state");
  field = build_decl (BUILTINS_LOCATION, FIELD_DECL, name,
		      object_state_ptr_type);
  DECL_CONTEXT(field) = union_type__;
  DECL_CHAIN(last_field) = field;
  last_field = field;

  name = get_identifier ("literal");
  field = build_decl (BUILTINS_LOCATION, FIELD_DECL, name,
		      ptr_type_node);
  DECL_CONTEXT(field) = union_type__;
  DECL_CHAIN(last_field) = field;
  last_field = field;

  layout_type (union_type__);
  
  name = get_identifier("o");
  tree union_type_decl = build_decl(BUILTINS_LOCATION, TYPE_DECL, name,
				    union_type__);
  DECL_ARTIFICIAL(union_type_decl) = 1;
  TYPE_NAME(union_type__) = union_type_decl;
  gpy_preserve_from_gc(union_type_decl);
  rest_of_decl_compilation(union_type_decl, 1, 0);

  tree gpy_object_struct_Type = make_node (RECORD_TYPE);
  
  name = get_identifier("type");
  field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name, integer_type_node);
  DECL_CONTEXT(field) = gpy_object_struct_Type;
  DECL_CHAIN(last_field) = field;
  last_field = field;

  field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
		     union_type_decl);
  DECL_CONTEXT(field) = gpy_object_struct_Type;
  DECL_CHAIN(last_field) = field;
  last_field = field;

  layout_type (object_state_struct_Type);
  
  name = get_identifier ("gpy_object_t");
  tree gpy_object_type_decl = build_decl(BUILTINS_LOCATION, TYPE_DECL, name,
					 gpy_object_struct_Type);
  DECL_ARTIFICIAL(gpy_object_type_decl) = 1;
  TYPE_NAME(gpy_object_struct_Type) = name;
  gpy_preserve_from_gc(gpy_object_type_decl);
  rest_of_decl_compilation(gpy_object_type_decl, 1, 0);

  return build_pointer_type (gpy_object_struct_Type);
}

tree gpy_dot_type_const_string_tree (const char * str)
{
  tree index_type = build_index_type (size_int (strlen (str)));
  tree const_char_type = build_qualified_type (unsigned_char_type_node,
					       TYPE_QUAL_CONST);
  tree string_type = build_array_type (const_char_type, index_type);
  string_type = build_variant_type_copy (string_type);
  TYPE_STRING_FLAG (string_type) = 1;
  tree string_val = build_string (strlen (str), str);
  TREE_TYPE (string_val) = string_type;
  return string_val;
}

void gpy_dot_types_init (void)
{
  gpy_builtin_types_vec = VEC_alloc (tree,gc,0);

  tree const_char_type = build_qualified_type (unsigned_char_type_node,
					       TYPE_QUAL_CONST);
  tree ctype = build_pointer_type (const_char_type);

  VEC_safe_push (tree, gc, gpy_builtin_types_vec,
		 gpy_build_py_object_type ());
  gpy_preserve_from_gc (gpy_object_type_ptr);

  VEC_safe_push (tree, gc, gpy_builtin_types_vec,
		 build_pointer_type (gpy_object_type_ptr));
  gpy_preserve_from_gc (gpy_object_type_ptr_ptr);
  
  VEC_safe_push (tree, gc, gpy_builtin_types_vec, ctype);
  gpy_preserve_from_gc (gpy_const_char_ptr);

  VEC_safe_push (tree, gc, gpy_builtin_types_vec,
		 gpy_build_py_vector_type ());
  gpy_preserve_from_gc (gpy_vector_type);

  VEC_safe_push (tree, gc, gpy_builtin_types_vec,
		 build_pointer_type (gpy_vector_type));
  gpy_preserve_from_gc (gpy_vector_type_ptr);

  VEC_safe_push (tree, gc, gpy_builtin_types_vec,
		 build_pointer_type (gpy_vector_type_ptr));
  gpy_preserve_from_gc (gpy_vector_type_ptr_ptr);

  VEC_safe_push (tree, gc, gpy_builtin_types_vec,
		 gpy_build_py_attrib_type ());
  gpy_preserve_from_gc (gpy_attrib_type);

  VEC_safe_push (tree, gc, gpy_builtin_types_vec,
		 build_pointer_type (gpy_attrib_type));
  gpy_preserve_from_gc (gpy_attrib_type_ptr);

  VEC_safe_push (tree, gc, gpy_builtin_types_vec,
		 build_pointer_type (gpy_attrib_type_ptr));
  gpy_preserve_from_gc (gpy_attrib_type_ptr_ptr);
}

[+ ESAC +]