summaryrefslogtreecommitdiff
path: root/gcc/testsuite/melt/t-treecode.melt
blob: 09c7ac048293367e1fd123d073004f8fbc1fc112 (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
;; file t-treecode.melt -*- lisp -*- this sample code is GPLv3, it
;; will go into libmelt-ana-tree.melt as soon as it works.
#| run in buildir/gcc
 ln -sv $GCCMELT_SOURCE/gcc/testsuite/melt/t-treecode.melt

 ./cc1 -fmelt-mode=runfile @meltbuild-common.args -Iinclude/ \
    -fmelt-init=@melt-default-modules.quicklybuilt -fmelt-arg=t-treecode.melt \
     /dev/null -o /dev/null
## perhaps also: -fmelt-debugging=all -fmelt-out-descr-comment

or in a MELT module

gcc -fplugin=melt -fplugin-arg-melt-mode=runfile @meltbuild-common.args \
    -fplugin-arg-melt-init=@melt-default-modules.quicklybuilt -fplugin-arg-melt-arg=t-treecode.melt \
     -c -x c /dev/null -o /dev/null
|#
(comment "***
    Copyright  2014  Free Software Foundation, Inc.
    Contributed by Basile Starynkevitch <basile@starynkevitch.net>

    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/>.
***")

(module_is_gpl_compatible "GPLv3")


(let (      
      (:macro  gcc_tree_code (sexp env mexpander modctx)	      
	       (debug "gcc_tree_code:macro2" " sexp=" sexp)
	       (let (
		     (sloc (get_field :loca_location sexp))
		     (sexcont (get_field :sexp_contents sexp))
		     (xargs (expand_restlist_as_tuple sexcont env mexpander modctx))
		     (namecod (multiple_nth xargs 0))
		     (strcod (multiple_nth xargs 1))
		     (typcod (multiple_nth xargs 2))
		     (nargcod (multiple_nth xargs 3))
		     (nbargs (multiple_length xargs))
		     (namev (get_field :named_name namecod))
		     (treenamev (string4out discr_string "tree_" namev))
		     (treesymb (create_symbolstr treenamev))
		     (statesymb (create_symbolstr (string4out discr_string "treestate_" namev)))
		     )
		 (when (!=i 4 nbargs)
		   (error_at sloc "GCC_TREE_CODE need four arguments, got $1" xargs)
		   (return))
		 (debug "gcc_tree_code:macro2"
			" namecod=" namecod
			" strcod=" strcod
			" typcod=" typcod
			" nargcod=" nargcod
			"\n.. treesymb=" treesymb
			"\n.. bind:" (find_env env treesymb))
		 (assert_msg "check namev" (is_string namev) namev namecod sexp)
		 (unless (find_env env treesymb)
		   (cond
		    ;; tcc_exceptional, no automatic sub-trees
		    ( (== typcod 'tcc_exceptional)
		      (let
			  (
			   (xcmatchdoc
			    (let
				(
				 (d
				  (substitute_sexpr
				   '#{Generated tcc_exceptional $TREESYMB cmatcher.}#
				   (lambda (symb)
				     (debug "gcc_tree_code:macro2-lambdasubstdoc symb=" symb)
				     (if (== symb 'treesymb)
					 (get_field :named_name treesymb)
				       symb))))
				 )
			      (debug "gcc_tree_code:macro2" " sloc=" sloc " exceptional xcmatchdoc=" d)
			      d))
			   (xmatexp
			    (let
				(
				 (e
				  (substitute_sexpr
				   '#{ /* generated exceptional $TREESYMB tester ? */
				   $TR && TREE_CODE($TR) == $NAMECOD }#
				   (lambda (symb)
				     (debug "gcc_tree_code:macro2-lambdasubstexp symb=" symb)
				     (cond
				      ( (== symb 'treesymb)
					(clone_with_discriminant (get_field :named_name treesymb) discr_verbatim_string)
					)
				      ( (== symb 'namecod)
					(clone_with_discriminant (get_field :named_name namecod) discr_verbatim_string)
					)
				      (:else
				       symb)))
				   ))
				 )
			      (debug "gcc_tree_code:macro2" " sloc=" sloc " exceptional xmatexp=" e)
			      e))
			   (xcmatcher
			    `(defcmatcher ,treesymb
			       (:tree tr)
			       ()
			       ,statesymb
			       :doc ,xcmatchdoc
			       ;; match-expander
			       ,xmatexp
			       ))
			   (xexport `(export_values ,treesymb))
			   )
			(put_fields xcmatcher :loca_location sloc)
			(put_fields xexport :loca_location sloc)
			(debug "gcc_tree_code:macro2" " sloc=" sloc " exceptional xcmatcher=" debug_more xcmatcher
			       "\n.. xexport=" xexport)
			(let (
			      (rmatch (macroexpand_1 xcmatcher env mexpander modctx))
			      (rexport (macroexpand_1 xexport env mexpander modctx))
			      )
			  (debug "gcc_tree_code:macro2" " sloc=" sloc " exceptional rmatch=" rmatch
				 "\n.. rexport=" rexport)
			  (return rmatch (tuple rexport))
			  ))
		      )
		    ;; tcc_constant & tcc_declaration, without sub-trees
		    (
		     (or (== typcod 'tcc_constant)
			 (== typcod 'tcc_declaration)
			 )
		     (if (>i (get_int nargcod) 0)
			 (error_at sloc "GCC_TREE_CODE $1 of tcc_constant or tcc_declaration type with non-zero arity $2"
				   treesymb nargcod
				   ))
		     (let
			 (
			  (xcmatchdoc
			   (let
			       (
				(d
				 (substitute_sexpr
				  '#{Generated simple $TYPCOD $TREESYMB cmatcher.}#
				  (lambda (symb)
				    (debug "gcc_tree_code:macro2-lambdasubsttypcod symb=" symb)
				    (if (== symb 'typcod)
					(get_field :named_name typcod)
				      symb))))
				)
			     (debug "gcc_tree_code:macro2"" sloc=" sloc "; constdecl xcmatchdoc=" d)
			     d))
			  (xmatexp
			   (let
			       (
				(e
				 (substitute_sexpr
				  '#{ /* generated simple $TREESYMB tester ? */
				  $TR && TREE_CODE($TR) == $NAMECOD }#
				  (lambda (symb)
				    (debug "gcc_tree_code:macro2-lambdasubsttypcod symb=" symb)
				    (cond
				     ( (== symb 'treesymb)
				       (clone_with_discriminant (get_field :named_name treesymb) discr_verbatim_string)
				       )
				     ( (== symb 'namecod)
				       (clone_with_discriminant (get_field :named_name namecod) discr_verbatim_string)
				       )
				     (:else
				      symb)))
				  ))
				)
			     (debug "gcc_tree_code:macro2"" sloc=" sloc "; constdecl xmatexp=" e)
			     e))
			  (xcmatcher
			   `(defcmatcher ,treesymb
			      (:tree tr)
			      ()
			      ,statesymb
			      :doc ,xcmatchdoc
			      ;; match-expander
			      ,xmatexp
			      ))
			  (xexport `(export_values ,treesymb))
			  )
		       (put_fields xcmatcher :loca_location sloc)
		       (put_fields xexport :loca_location sloc)
		       (debug "gcc_tree_code:macro2" " sloc=" sloc "; constdecl xcmatcher=" debug_more xcmatcher
			      "\n.. xexport=" xexport)
		       (let (
			     (rmatch (macroexpand_1 xcmatcher env mexpander modctx))
			     (rexport (macroexpand_1 xexport env mexpander modctx))
			     )
			 (debug "gcc_tree_code:macro2" " sloc=" sloc "; rmatch=" rmatch
				"\n.. rexport=" rexport)
			 (return rmatch (tuple rexport))
			 ))
		     
		     )
		    ;; For fixed tcc_reference, tcc_expression,
		    ;; tcc_comparison, tcc_unary, tcc_binary,
		    ;; tcc_statement.  They use the arity fourth
		    ;; argument, and we extract sub-tree operands.
		    (
		     (or (== typcod 'tcc_reference)
			 (== typcod 'tcc_expression)
			 (== typcod 'tcc_unary)
			 (== typcod 'tcc_binary)
			 (== typcod 'tcc_statement)
			 )
		     (let
			 (
			  (xcmatchdoc
			   (let
			       (
				(d
				 (substitute_sexpr
				  '#{Generated fixed $TYPCOD $TREESYMB cmatcher of arity $NARGCOD.}#
				  (lambda (symb)
				    (debug "gcc_tree_code:macro2-lambdasubstfixed symb=" symb)
				    (cond
				     ( (== symb 'typcod)
				       typcod)
				     ( (== symb 'nargcod)
				       nargcod)
				     (:else symb))
				    )))
				)
			     (debug "gcc_tree_code:macro2" " sloc=" sloc "; fixed xcmatchdoc=" d)
			     d))
			  (xmatexp
			   (let
			       (
				(e
				 (substitute_sexpr
				  '#{ /* generated fixed $TREESYMB tester ? */
				  $TR && TREE_CODE($TR) == $NAMECOD }#
				  (lambda (symb)
				    (debug "gcc_tree_code:macro2-lambdasubstfixed symb=" symb)
				    (cond
				     ( (== symb 'treesymb)
				       (clone_with_discriminant (get_field :named_name treesymb) discr_verbatim_string)
				       )
				     ( (== symb 'namecod)
				       (clone_with_discriminant (get_field :named_name namecod) discr_verbatim_string)
				       )
				     ( (== symb 'nargcod)
				       nargcod)
				     (:else
				      symb)))
				  ))
				)
			     (debug "gcc_tree_code:macro2" " sloc=" sloc "; fixed xmatexp=" e)
			     e))
			  (xlistfill (make_list discr_list))
			  (tupopervar
			   (let ( (:long narity (get_int nargcod))
				  ( tv (make_multiple discr_multiple narity))
				  )
			     (foreach_long_upto
			      (1 narity)
			      (:long ix)
			      (let ( (:long previx (-i ix 1))
				     (opnamstr (string4out discr_string "TROPERAND" previx))
				     (opnamsymb (create_symbolstr opnamstr))
				     )
				(multiple_put_nth tv previx opnamsymb)
				(let ( (tr 'tr)
				       )
				  (add2list xlistfill ##{ $OPNAMSYMB = TREE_OPERAND($TR, $PREVIX)}#
					    ";\n")
				  )
				)
			      (debug "gcc_tree_code:macro2" " sloc=" sloc "; fixed tupopervar=" tv
				     "\n.. updated xlistfill=" xlistfill)
			      tv
			      )))
			  (xmatfill
			   (let ( (e (substitute_sexpr
				      '#{ /* generated fixed $TREESYMB filler ! */
				      $XLISTFILL
				      /* end of generated fixed $TREESYMB filler */}#
				      (lambda (symb)
					(debug "gcc_tree_code:macro2-lambdasubstfixfiller symb=" symb)
					(cond
					 ( (== symb 'treesymb)
					   (clone_with_discriminant (get_field :named_name treesymb) discr_verbatim_string)
					   )
					 ( (== symb 'xlistfill)
					   xlistfill)
					 (:else symb))
					)))
				  )
			     (debug "gcc_tree_code:macro2" " sloc=" sloc "; fixed xmatfill=" e)
			     e
			     )
			   )
			  (xcmatcher
			   (let ( (m
				   `(defcmatcher ,treesymb
				      (:tree tr)
				      (:tree ,tupopervar)
				      ,statesymb
				      :doc ,xcmatchdoc
				      ;; match-expander
				      ,xmatexp
				      ;; match-filler
				      ,xmatfill
				      ))
				  )
			     (debug "gcc_tree_code:macro2" " sloc=" sloc "; fixed xcmatcher=" m)
			     m))
			  (xexport `(export_values ,treesymb))
			  )
		       (put_fields xcmatcher :loca_location sloc)
		       (put_fields xexport :loca_location sloc)
		       (debug "gcc_tree_code:macro2" " sloc=" sloc "; " "@fixed xcmatcher=" debug_more xcmatcher
			      "\n.. xexport=" xexport)
		       (let (
			     (rmatch (macroexpand_1 xcmatcher env mexpander modctx))
			     (rexport (macroexpand_1 xexport env mexpander modctx))
			     )
			 (debug "gcc_tree_code:macro2" " rmatch=" rmatch
				"\n.. rexport=" rexport)
			 (return rmatch (tuple rexport))
			 ))
		     )
		    ;; otherwise
		    ( :else
		      (warning_at sloc "MELT should define $1 for tree code of type $2 arity $3"
				  treesymb typcod nargcod)
		      )
		    )
		   )
		 (error_at sloc "unexpected GCC_TREE_CODE for $1" namev)
		 (return ()) ;;  temporarily, happens for non-handled cases
		 )
	       )
      )
  ;; for testing, these are not defined in libmelt-ana-tree.melt
  (gcc_tree_code TREE_BINFO "tree_binfo" tcc_exceptional 0)
  (gcc_tree_code INDIRECT_REF "indirect_ref" tcc_reference 1)
  (gcc_tree_code TRUTH_ORIF_EXPR "truth_orif_expr" tcc_expression 2)
  (gcc_tree_code WIDEN_SUM_EXPR "widen_sum_expr" tcc_binary 2)
  
  #|
  (debug "before loading2 libmelt-treecode")
  (skip_nil (load "_libmelt-treecode.melt"))
  (debug "after loading2 libmelt-treecode")
  |#
  )

;; eof t-treecode.melt