summaryrefslogtreecommitdiff
path: root/gcc/python/py-lang.c
blob: 37fa27be1b26204838a94fc8c76ce707b9e1808a (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
/* 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/>. */

#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-dot.h"
#include "py-vec.h"
#include "py-tree.h"

/* Language-dependent contents of a type.  */
struct GTY(()) lang_type {
  char dummy;
} ;

/* Language-dependent contents of a decl.  */
struct GTY(()) lang_decl {
  char dummy;
} ;

/* Language-dependent contents of an identifier.  This must include a
   tree_identifier.
*/
struct GTY(()) lang_identifier {
  struct tree_identifier common;
} ;

/* The resulting tree type.  */
union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
	   chain_next ("(union lang_tree_node *) TREE_CHAIN (&%h.generic)")))
lang_tree_node
{
  union tree_node GTY((tag ("0"),
		       desc ("tree_node_structure (&%h)"))) generic;
  struct lang_identifier GTY((tag ("1"))) identifier;
};

/* We don't use language_function.  */
struct GTY(()) language_function {
  int dummy;
};

/* Language hooks.  */
static
bool gpy_langhook_init (void)
{
  build_common_tree_nodes( false );
  build_common_tree_nodes_2( 0 );

  void_list_node = build_tree_list (NULL_TREE, void_type_node);

  gpy_initilize_types ();

  return true;
}

/* Initialize before parsing options.  */
static void
gpy_langhook_init_options (unsigned int decoded_options_count,
			   struct cl_decoded_option *decoded_options)
{
  return;
}

/* Handle gpy specific options.  Return 0 if we didn't do anything.  */
static bool
gpy_langhook_handle_option (size_t scode, const char *arg, int value, int kind,
			    location_t l, const struct cl_option_handlers * handlers)
{
  enum opt_code code = (enum opt_code) scode;
  int retval = 1;

  switch( code )
    {
    default:
      /* Just return 1 to indicate that the option is valid.  */
      break;
    }

  return retval;
}

/* Run after parsing options.  */
static bool
gpy_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED)
{
  debug("post options!\n");
  gcc_assert( num_in_fnames > 0 );

  if( flag_excess_precision_cmdline == EXCESS_PRECISION_DEFAULT )
    {
      flag_excess_precision_cmdline = EXCESS_PRECISION_STANDARD;
    }

  /* Returning false means that the backend should be used.  */
  return false;
}

static void
gpy_langhook_parse_file (void)
{
  unsigned int idx = 0;
  debug("parse file!\n");

  for ( ; idx<num_in_fnames; ++idx )
    {
      const char * t = in_fnames[idx];
      debug("<%i> input = <%s>!\n", idx, t);
      gpy_lex_parse( t );
    }
}

static tree
gpy_langhook_type_for_size( unsigned int bits ATTRIBUTE_UNUSED,
			    int unsignedp ATTRIBUTE_UNUSED )
{
  debug("type for size!\n");
  return NULL;
}

static tree
gpy_langhook_type_for_mode( enum machine_mode mode ATTRIBUTE_UNUSED,
			    int unsignedp ATTRIBUTE_UNUSED )
{
  debug("type for mode!\n");
  return NULL_TREE;
}

/* Record a builtin function.  We just ignore builtin functions.  */
static tree
gpy_langhook_builtin_function( tree decl ATTRIBUTE_UNUSED )
{
  debug("builtin function!\n");
  return decl;
}

static int
gpy_langhook_global_bindings_p( void )
{
  debug("global bindings!\n");
  return 1;
}

static tree
gpy_langhook_pushdecl( tree decl ATTRIBUTE_UNUSED )
{
  debug("pushdecl!\n");
  gcc_unreachable ();
  return NULL;
}

static tree
gpy_langhook_getdecls( void )
{
  debug("get decls!\n");
  return NULL;
}

/* Write out globals.  */
static
void gpy_langhook_write_globals (void)
{
  gpy_stmt_write_globals ();
}

static int
gpy_langhook_gimplify_expr( tree *expr_p ATTRIBUTE_UNUSED,
			    gimple_seq *pre_p ATTRIBUTE_UNUSED,
			    gimple_seq *post_p ATTRIBUTE_UNUSED )
{
  // debug_tree( (*expr_p) );
  enum tree_code code = TREE_CODE (*expr_p);

  /* This is handled mostly by gimplify.c, but we have to deal with
     not warning about int x = x; as it is a GCC extension to turn off
     this warning but only if warn_init_self is zero.  */
  if (code == DECL_EXPR
      && TREE_CODE (DECL_EXPR_DECL (*expr_p)) == VAR_DECL
      && !DECL_EXTERNAL (DECL_EXPR_DECL (*expr_p))
      && !TREE_STATIC (DECL_EXPR_DECL (*expr_p))
      && (DECL_INITIAL (DECL_EXPR_DECL (*expr_p)) == DECL_EXPR_DECL (*expr_p))
      && !warn_init_self)
    TREE_NO_WARNING (DECL_EXPR_DECL (*expr_p)) = 1;

  return GS_UNHANDLED;
}

/* Functions called directly by the generic backend.  */
tree convert( tree type ATTRIBUTE_UNUSED,
	      tree expr ATTRIBUTE_UNUSED )
{
  gcc_unreachable( );
}

static GTY(()) tree gpy_gc_root;

void gpy_preserve_from_gc (tree t)
{
  gpy_gc_root = tree_cons (NULL_TREE, t, gpy_gc_root);
}

/* The attribute table might be used for the GCC attribute syntax, e.g.
 * __attribute__((unused)), but this feature isn't yet used in gcalc
 */ 
const struct attribute_spec python_attribute_table[] = {
  { NULL, 0, 0, false, false, false, NULL }
};


/* The language hooks data structure. This is the main interface between the GCC front-end
 * and the GCC middle-end/back-end. A list of language hooks could be found in
 * <gcc>/langhooks.h
 */
#undef LANG_HOOKS_NAME
#undef LANG_HOOKS_INIT 
#undef LANG_HOOKS_INIT_OPTIONS
#undef LANG_HOOKS_HANDLE_OPTION
#undef LANG_HOOKS_POST_OPTIONS
#undef LANG_HOOKS_PARSE_FILE
#undef LANG_HOOKS_TYPE_FOR_MODE
#undef LANG_HOOKS_TYPE_FOR_SIZE
#undef LANG_HOOKS_BUILTIN_FUNCTION
#undef LANG_HOOKS_GLOBAL_BINDINGS_P
#undef LANG_HOOKS_PUSHDECL
#undef LANG_HOOKS_GETDECLS
#undef LANG_HOOKS_WRITE_GLOBALS
#undef LANG_HOOKS_GIMPLIFY_EXPR

#define LANG_HOOKS_NAME			"GNU Python"
#define LANG_HOOKS_INIT			gpy_langhook_init
#define LANG_HOOKS_INIT_OPTIONS		gpy_langhook_init_options
#define LANG_HOOKS_HANDLE_OPTION	gpy_langhook_handle_option
#define LANG_HOOKS_POST_OPTIONS		gpy_langhook_post_options
#define LANG_HOOKS_PARSE_FILE		gpy_langhook_parse_file
#define LANG_HOOKS_TYPE_FOR_MODE	gpy_langhook_type_for_mode
#define LANG_HOOKS_TYPE_FOR_SIZE	gpy_langhook_type_for_size
#define LANG_HOOKS_BUILTIN_FUNCTION	gpy_langhook_builtin_function
#define LANG_HOOKS_GLOBAL_BINDINGS_P	gpy_langhook_global_bindings_p
#define LANG_HOOKS_PUSHDECL		gpy_langhook_pushdecl
#define LANG_HOOKS_GETDECLS		gpy_langhook_getdecls
#define LANG_HOOKS_WRITE_GLOBALS	gpy_langhook_write_globals
#define LANG_HOOKS_GIMPLIFY_EXPR	gpy_langhook_gimplify_expr

struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;

#include "gt-python-py-lang.h"
#include "gtype-python.h"