tagged release 0.6.4
[parrot.git] / compilers / pct / src / POST / Node.pir
blobe8ca3e5e02ce241d533c400ac388ed4328bc58c1
1 =head1 NAME
3 POST - Parrot opcode syntax tree
5 =head1 DESCRIPTION
7 This file implements the various opcode syntax tree nodes
8 for compiling programs in Parrot.
10 =cut
12 .namespace [ 'POST' ]
14 .sub '__onload' :load :init
15     .local pmc p6meta, base
17     ##  create POST classes
18     p6meta = new 'P6metaclass'
19     base = p6meta.'new_class'('POST::Node', 'parent'=>'PCT::Node')
20     p6meta.'new_class'('POST::Op', 'parent'=>base)
21     p6meta.'new_class'('POST::Ops', 'parent'=>base)
22     p6meta.'new_class'('POST::Label', 'parent'=>base)
23     p6meta.'new_class'('POST::Sub', 'parent'=>base)
25     $P0 = new 'ResizableStringArray'
26     $P0[0] = "    .param pmc %0"
27     $P0[1] = "    .param pmc %0 :optional\n    .param int has_%0 :opt_flag"
28     $P0[2] = "    .param pmc %0 :slurpy"
29     $P0[4] = "    .param pmc %0 :named(%1)"
30     $P0[5] = "    .param pmc %0 :optional :named(%1)\n    .param int has_%0 :opt_flag"
31     $P0[6] = "    .param pmc %0 :slurpy :named"
32     set_hll_global ['POST::Sub'], '%!paramfmt', $P0
33     .return ()
34 .end
37 =head1 POST Node types
39 =head2 POST::Node
41 C<POST::Node> is the base class for all POST nodes.  It's derived from class
42 C<PCT::Node> (see F<compilers/pct/src/PCT/Node.pir>).
44 =over 4
46 =item result([value])
48 Get/set
50 =cut
52 .namespace [ 'POST::Node' ]
54 =item result([value])
56 Get or set the result value of this node.  If the result value
57 is set to another POST node, then that node's result is used
58 as the result of the current node.
60 =cut
62 .sub 'result' :method
63     .param pmc value           :optional
64     .param int has_value       :opt_flag
66     if has_value == 1 goto set_value
67     value = self['result']
68     if null value goto result_null
69     $I0 = isa value, 'POST::Node'
70     if $I0 goto result_node
71     .return (value)
72   result_node:
73     .return value.'result'()
74   result_null:
75     .return ('')
76   set_value:
77     self['result'] = value
78     .return (value)
79 .end
82 =item get_string()   # vtable method
84 Returns the result of the current node as a string.
86 =cut
88 .sub 'get_string' :vtable :method
89     $S0 = self.'result'()
90     .return ($S0)
91 .end
94 =item push_pirop(pirop [,arglist :slurpy] [,adverbs :slurpy :named])
96 Shortcut for creating and adding a new POST::Op node with opcode
97 C<pirop> and any supplied arguments or options.  Returns the
98 newly created node.
100 =cut
102 .sub 'push_pirop' :method
103     .param pmc pirop
104     .param pmc arglist         :slurpy
105     .param pmc adverbs         :slurpy :named
106     adverbs['pirop'] = pirop
107     $P0 = get_hll_global ['POST'], 'Op'
108     $P1 = $P0.'new'(arglist :flat, adverbs :flat :named)
109     self.'push'($P1)
110     .return ($P1)
111 .end
114 =item escape(str)
116 Return C<str> as a PIR constant string.
118 =cut
120 .sub 'escape' :method
121     .param string str
122     $P0 = new 'CodeString'
123     str = $P0.'escape'(str)
124     .return (str)
125 .end
127 =back
129 =head2 POST::Op
131 C<POST::Op> nodes represents any PIR opcodes.
133 =over 4
135 =item pirop([opcode])
137 Get/set the opcode type for this node.
139 =cut
141 .namespace [ 'POST::Op' ]
143 .sub 'pirop' :method
144     .param pmc value           :optional
145     .param int has_value       :opt_flag
146     .return self.'attr'('pirop', value, has_value)
147 .end
149 .sub 'inline' :method
150     .param pmc value           :optional
151     .param int has_value       :opt_flag
152     .return self.'attr'('inline', value, has_value)
153 .end
156 .namespace [ 'POST::Label' ]
158 .sub 'result' :method
159     .param pmc value           :optional
160     .param int has_value       :opt_flag
161     if has_value goto set_value
162     value = self['result']
163     unless null value goto end
164     .local string name
165     name = self.'name'()
166     value = self.'unique'(name)
167   set_value:
168     self['result'] = value
169   end:
170     .return (value)
171 .end
174 .namespace [ 'POST::Sub' ]
176 .sub 'blocktype' :method
177     .param pmc value           :optional
178     .param int has_value       :opt_flag
179     .return self.'attr'('blocktype', value, has_value)
180 .end
183 .sub 'namespace' :method
184     .param pmc value           :optional
185     .param int has_value       :opt_flag
186     .return self.'attr'('namespace', value, has_value)
187 .end
190 .sub 'outer' :method
191     .param pmc value           :optional
192     .param int has_value       :opt_flag
193     .return self.'attr'('outer', value, has_value)
194 .end
197 .sub 'lexid' :method
198     .param pmc value           :optional
199     .param int has_value       :opt_flag
200     .return self.'attr'('lexid', value, has_value)
201 .end
204 .sub 'pirflags' :method
205     .param pmc value           :optional
206     .param int has_value       :opt_flag
207     .return self.'attr'('pirflags', value, has_value)
208 .end
211 .sub 'compiler' :method
212     .param pmc value           :optional
213     .param int has_value       :opt_flag
214     .return self.'attr'('compiler', value, has_value)
215 .end
218 .sub 'compiler_args' :method
219     .param pmc value           :optional
220     .param int has_value       :opt_flag
221     .return self.'attr'('compiler_args', value, has_value)
222 .end
225 .sub 'add_param' :method
226     .param pmc pname
227     .param pmc adverbs         :slurpy :named
229     .local int optional, slurpy
230     .local string named
231     optional = adverbs['optional']
232     slurpy = adverbs['slurpy']
233     named = adverbs['named']
235     .local int paramseq
236     paramseq = isne optional, 0
237     unless slurpy goto slurpy_done
238     paramseq += 2
239   slurpy_done:
240     unless named goto named_done
241     paramseq += 4
242   named_done:
244     .local pmc paramlist
245     paramlist = self['paramlist']
246     unless null paramlist goto have_paramlist
247     paramlist = new 'ResizablePMCArray'
248     self['paramlist'] = paramlist
249   have_paramlist:
251     .local pmc code
252     code = paramlist[paramseq]
253     unless null code goto have_code
254     code = new 'CodeString'
255     paramlist[paramseq] = code
256   have_code:
258     .local pmc paramfmt
259     paramfmt = get_hll_global ['POST::Sub'], '%!paramfmt'
260     $S0 = paramfmt[paramseq]
261     named = code.'escape'(named)
262     code.'emit'($S0, pname, named)
264     .return ()
265 .end
267 =back
269 =head1 AUTHOR
271 Patrick Michaud <pmichaud@pobox.com> is the author and maintainer.
272 Please send patches and suggestions to the Parrot porters or
273 Perl 6 compilers mailing lists.
275 =head1 HISTORY
277 2007-11-21  Significant refactor as part of Parrot Compiler Toolkit
279 =head1 COPYRIGHT
281 Copyright (C) 2006-2008, The Perl Foundation.
283 =cut
285 # Local Variables:
286 #   mode: pir
287 #   fill-column: 100
288 # End:
289 # vim: expandtab shiftwidth=4 ft=pir: