├── .gitignore
├── COPYING
├── Makefile
├── README.rst
├── location.h
├── main.cc
├── regvm.cc
├── regvm.h
├── stackvm.cc
└── stackvm.h
/.gitignore:
--------------------------------------------------------------------------------
1 | *~
2 | *.o
3 |
--------------------------------------------------------------------------------
/COPYING:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | demo: jittest
2 | ./jittest
3 |
4 | SOURCE_FILES:=stackvm.cc regvm.cc main.cc
5 | OBJECT_FILES:=stackvm.o regvm.o main.o
6 |
7 | $(OBJECT_FILES): %.o: %.cc stackvm.h regvm.h
8 | g++ -c -o $@ -g $< -Wall
9 |
10 | jittest: $(OBJECT_FILES)
11 | g++ -o $@ $(OBJECT_FILES) -lgccjit
12 |
13 | clean:
14 | rm -f *.o jittest
15 |
--------------------------------------------------------------------------------
/README.rst:
--------------------------------------------------------------------------------
1 | This is a simple testbed intended for experiments with JIT compilation
2 |
3 | It builds an executable containing two virtual machines: ``stackvm`` and
4 | ``regvm``
5 |
6 | It uses my experimental libgccjit.so_ code to compile ``regvm`` code
7 | to machine code and run it in-process.
8 |
9 | .. _libgccjit.so: http://gcc.gnu.org/wiki/JIT/
10 |
11 | stackvm
12 | =======
13 | A simple virtual machine in which each frame has a stack of integer values.
14 | It runs bytecode programs. The following bytecodes are supported::
15 |
16 | DUP
17 | ROT
18 | PUSH_INT_CONST
19 | BINARY_INT_ADD
20 | BINARY_INT_SUBTRACT
21 | BINARY_INT_COMPARE_LT
22 | JUMP_ABS_IF_TRUE
23 | CALL_INT
24 | RETURN_INT
25 |
26 | It is intended as a (very simple) example of the kind of bytecode
27 | interpreter seen in dynamic languages such as Python, Ruby etc
28 |
29 | ``stackvm`` programs can be interpreted, disassembled, and compiled to
30 | ``regvm`` programs.
31 |
32 | Here's what a simple recursive Fibonacci program looks like in
33 | ``stackvm`` bytecode::
34 |
35 | [0] : DUP
36 | [1] : PUSH_INT_CONST 2
37 | [3] : BINARY_INT_COMPARE_LT
38 | [4] : JUMP_ABS_IF_TRUE 17
39 | [6] : DUP
40 | [7] : PUSH_INT_CONST 1
41 | [9] : BINARY_INT_SUBTRACT
42 | [10] : CALL_INT
43 | [11] : ROT
44 | [12] : PUSH_INT_CONST 2
45 | [14] : BINARY_INT_SUBTRACT
46 | [15] : CALL_INT
47 | [16] : BINARY_INT_ADD
48 | [17] : RETURN_INT
49 |
50 |
51 | regvm
52 | =====
53 | A simple virtual machine in which each frame has a set of integer
54 | "registers" (i.e. numbered local variables).
55 |
56 | Operations have one or two inputs, and zero or one outputs.
57 |
58 | Inputs are integers plus and addressing modes: either constant integer, or
59 | register index.
60 |
61 | The following operations are supported::
62 |
63 | COPY_INT
64 | BINARY_INT_ADD
65 | BINARY_INT_SUBTRACT
66 | BINARY_INT_COMPARE_LT
67 | JUMP_ABS_IF_TRUE
68 | CALL_INT
69 | RETURN_INT
70 |
71 | It is intended as a simpler VM from which to generate machine code.
72 |
73 | ``regvm`` programs can be interpreted and disassembled.
74 |
75 | The main program creates a simple recursive Fibonacci program using ``stackvm``,
76 | interprets it for one input, then compiles it to ``regvm``, and interprets that
77 | again for one input.
78 |
79 | Here's what the Fibonacci program looks like after it's been compiled to
80 | ``regvm`` code. Note that no optimization happens at this stage - it simply
81 | unrolls the stack manipulation into a set of "registers", where R0 is the
82 | bottom of the stack, R1 directly above it etc - so there's plenty of
83 | redundancy here::
84 |
85 | [0] : R0 = R0;
86 | [1] : R1 = R0;
87 | [2] : R2 = 2;
88 | [3] : R3 = R1 < R2;
89 | [4] : R1 = R3;
90 | [5] : IF (R1) GOTO 23;
91 | [6] : R0 = R0;
92 | [7] : R1 = R0;
93 | [8] : R2 = 1;
94 | [9] : R3 = R1 - R2;
95 | [10] : R1 = R3;
96 | [11] : R3 = CALL(R1);
97 | [12] : R1 = R3;
98 | [13] : R3 = R1;
99 | [14] : R1 = R0;
100 | [15] : R0 = R3;
101 | [16] : R2 = 2;
102 | [17] : R3 = R1 - R2;
103 | [18] : R1 = R3;
104 | [19] : R3 = CALL(R1);
105 | [20] : R1 = R3;
106 | [21] : R3 = R0 + R1;
107 | [22] : R0 = R3;
108 | [23] : RETURN(R0);
109 |
110 | This can be interpreted (by ``regvm.cc:vm::interpret``) or compiled (by
111 | ``regvm.cc:wordcode::compile``).
112 |
113 | The compiler uses my experimental libgccjit.so_ API for GCC.
114 |
115 | One of GCC's internal representations is called "gimple". A dump of the
116 | initial gimple representation of the code can be seen by setting::
117 |
118 | gcc_jit_context_set_bool_option (ctxt,
119 | GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE,
120 | 1);
121 |
122 | in ``wordcode::compile`` giving the following gimple dump, which closely
123 | resembles the ``regvm`` dump above (no optimization has happened yet. There
124 | is a label at every instruction, most of which are redundant as they aren't
125 | jump targets::
126 |
127 | fibonacci (signed int input)
128 | {
129 | D.83;
130 | D.84;
131 | signed int D.85;
132 |
133 | R0 = input;
134 | instr0:
135 | R0 = R0;
136 | instr1:
137 | R1 = R0;
138 | instr2:
139 | R2 = 2;
140 | instr3:
141 | D.83 = R1 < R2;
142 | R3 = (signed int) D.83;
143 | instr4:
144 | R1 = R3;
145 | instr5:
146 | D.84 = () R1;
147 | if (D.84 != 0) goto instr23; else goto instr6;
148 | instr6:
149 | R0 = R0;
150 | instr7:
151 | R1 = R0;
152 | instr8:
153 | R2 = 1;
154 | instr9:
155 | R3 = R1 - R2;
156 | instr10:
157 | R1 = R3;
158 | instr11:
159 | R3 = fibonacci (R1);
160 | instr12:
161 | R1 = R3;
162 | instr13:
163 | R3 = R1;
164 | instr14:
165 | R1 = R0;
166 | instr15:
167 | R0 = R3;
168 | instr16:
169 | R2 = 2;
170 | instr17:
171 | R3 = R1 - R2;
172 | instr18:
173 | R1 = R3;
174 | instr19:
175 | R3 = fibonacci (R1);
176 | instr20:
177 | R1 = R3;
178 | instr21:
179 | R3 = R0 + R1;
180 | instr22:
181 | R0 = R3;
182 | instr23:
183 | D.85 = R0;
184 | return D.85;
185 | }
186 |
187 | You can see the various optimization steps that ``gcc_jit_context_compile``
188 | performs by setting::
189 |
190 | gcc_jit_context_set_bool_option (ctxt,
191 | GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES,
192 | 1);
193 | gcc_jit_context_set_bool_option (ctxt,
194 | GCC_JIT_BOOL_OPTION_DUMP_EVERYTHING,
195 | 1);
196 |
197 | and reviewing the contents of the temporary directory it builds.
198 |
199 | For example, after optimizing, the gimple becomes::
200 |
201 | ;; Function fibonacci (fibonacci, funcdef_no=0, decl_uid=53, symbol_order=0)
202 |
203 | Removing basic block 8
204 | Removing basic block 9
205 | fibonacci (signed int input)
206 | {
207 | unsigned int _2;
208 | signed int _3;
209 | signed int add_acc_5;
210 | unsigned int _8;
211 | signed int acc_tmp_14;
212 | signed int add_acc_15;
213 | signed int add_acc_16;
214 | unsigned int _17;
215 | signed int add_acc_20;
216 | signed int _21;
217 | signed int _22;
218 | unsigned int _23;
219 | unsigned int _24;
220 | signed int _25;
221 |
222 | :
223 | if (input_4(D) <= 1)
224 | goto (instr23);
225 | else
226 | goto ;
227 |
228 | :
229 | goto (instr6);
230 |
231 | :
232 |
233 | # input_7 = PHI
234 | # add_acc_20 = PHI
235 | instr6:
236 | _23 = (unsigned int) input_7;
237 | _24 = _23 + 4294967295;
238 | _25 = (signed int) _24;
239 | R0_11 = fibonacci (_25);
240 | input_12 = input_7 + -2;
241 | add_acc_15 = add_acc_20 + R0_11;
242 | if (input_12 <= 1)
243 | goto ;
244 | else
245 | goto ;
246 |
247 | :
248 | # add_acc_16 = PHI
249 | _3 = input_4(D) + -2;
250 | _17 = (unsigned int) input_4(D);
251 | _8 = _17 + 4294967294;
252 | _2 = _8 >> 1;
253 | _21 = (signed int) _2;
254 | _22 = _21 * -2;
255 | input_13 = _3 + _22;
256 |
257 | # input_1 = PHI
258 | # add_acc_5 = PHI
259 | instr23:
260 | acc_tmp_14 = add_acc_5 + input_1;
261 | return acc_tmp_14;
262 |
263 | }
264 |
265 | Note how it's optimized away tail-recursion for one of the recursive
266 | invocations.
267 |
268 | The gimple representation is then converted to RTL (Register Transfer
269 | Language). In its initial state this looks like::
270 |
271 | ;;
272 | ;; Full RTL generated for this function:
273 | ;;
274 | (note 6 0 12 NOTE_INSN_DELETED)
275 | (note 12 6 7 2 [bb 2] NOTE_INSN_BASIC_BLOCK)
276 | (insn 7 12 8 2 (set (reg/v:SI 102 [ input ])
277 | (reg:SI 5 di [ input ])) -1
278 | (nil))
279 | (note 8 7 14 2 NOTE_INSN_FUNCTION_BEG)
280 | (insn 14 8 15 2 (set (reg:CCGC 17 flags)
281 | (compare:CCGC (reg/v:SI 102 [ input ])
282 | (const_int 1 [0x1]))) -1
283 | (nil))
284 | (jump_insn 15 14 16 2 (set (pc)
285 | (if_then_else (le (reg:CCGC 17 flags)
286 | (const_int 0 [0]))
287 | (label_ref:DI 56)
288 | (pc))) 616 {*jcc_1}
289 | (int_list:REG_BR_PROB 900 (nil))
290 | -> 56)
291 | (note 16 15 9 4 [bb 4] NOTE_INSN_BASIC_BLOCK)
292 | (insn 9 16 10 4 (set (reg/v:SI 90 [ input ])
293 | (reg/v:SI 102 [ input ])) -1
294 | (nil))
295 | (insn 10 9 17 4 (set (reg:SI 94 [ D.99 ])
296 | (const_int 0 [0])) -1
297 | (nil))
298 | (jump_insn 17 10 18 4 (set (pc)
299 | (label_ref 20)) -1
300 | (nil)
301 | -> 20)
302 | (barrier 18 17 28)
303 | (code_label 28 18 19 5 4 "" [1 uses])
304 | (note 19 28 20 5 [bb 5] NOTE_INSN_BASIC_BLOCK)
305 | (code_label 20 19 21 6 3 ("instr6") [1 uses])
306 | (note 21 20 22 6 [bb 6] NOTE_INSN_BASIC_BLOCK)
307 | (insn 22 21 23 6 (parallel [
308 | (set (reg:SI 103 [ D.98 ])
309 | (plus:SI (reg/v:SI 90 [ input ])
310 | (const_int -1 [0xffffffffffffffff])))
311 | (clobber (reg:CC 17 flags))
312 | ]) -1
313 | (nil))
314 | (insn 23 22 24 6 (set (reg:SI 5 di)
315 | (reg:SI 103 [ D.98 ])) -1
316 | (nil))
317 | (call_insn 24 23 25 6 (set (reg:SI 0 ax)
318 | (call (mem:QI (symbol_ref:DI ("fibonacci") [flags 0x1] ) [0 fibonacci S1 A8])
319 | (const_int 0 [0]))) -1
320 | (expr_list:REG_EH_REGION (const_int 0 [0])
321 | (nil))
322 | (expr_list:REG_BR_PRED (use (reg:SI 5 di))
323 | (nil)))
324 | (insn 25 24 26 6 (set (reg/v:SI 92 [ R0 ])
325 | (reg:SI 0 ax)) -1
326 | (nil))
327 | (insn 26 25 27 6 (parallel [
328 | (set (reg/v:SI 90 [ input ])
329 | (plus:SI (reg/v:SI 90 [ input ])
330 | (const_int -2 [0xfffffffffffffffe])))
331 | (clobber (reg:CC 17 flags))
332 | ]) -1
333 | (nil))
334 | (insn 27 26 29 6 (parallel [
335 | (set (reg:SI 94 [ D.99 ])
336 | (plus:SI (reg:SI 94 [ D.99 ])
337 | (reg/v:SI 92 [ R0 ])))
338 | (clobber (reg:CC 17 flags))
339 | ]) -1
340 | (nil))
341 | (insn 29 27 30 6 (set (reg:CCGC 17 flags)
342 | (compare:CCGC (reg/v:SI 90 [ input ])
343 | (const_int 1 [0x1]))) -1
344 | (nil))
345 | (jump_insn 30 29 31 6 (set (pc)
346 | (if_then_else (gt (reg:CCGC 17 flags)
347 | (const_int 0 [0]))
348 | (label_ref 28)
349 | (pc))) -1
350 | (int_list:REG_BR_PROB 9100 (nil))
351 | -> 28)
352 | (note 31 30 32 7 [bb 7] NOTE_INSN_BASIC_BLOCK)
353 | (insn 32 31 33 7 (parallel [
354 | (set (reg:SI 89 [ D.99 ])
355 | (plus:SI (reg/v:SI 102 [ input ])
356 | (const_int -2 [0xfffffffffffffffe])))
357 | (clobber (reg:CC 17 flags))
358 | ]) -1
359 | (nil))
360 | (insn 33 32 34 7 (parallel [
361 | (set (reg:SI 104 [ D.98 ])
362 | (plus:SI (reg/v:SI 102 [ input ])
363 | (const_int -2 [0xfffffffffffffffe])))
364 | (clobber (reg:CC 17 flags))
365 | ]) -1
366 | (nil))
367 | (insn 34 33 35 7 (parallel [
368 | (set (reg:SI 105 [ D.98 ])
369 | (lshiftrt:SI (reg:SI 104 [ D.98 ])
370 | (const_int 1 [0x1])))
371 | (clobber (reg:CC 17 flags))
372 | ]) -1
373 | (nil))
374 | (insn 35 34 36 7 (set (reg:SI 106)
375 | (const_int 0 [0])) -1
376 | (nil))
377 | (insn 36 35 37 7 (parallel [
378 | (set (reg:SI 107)
379 | (minus:SI (reg:SI 106)
380 | (reg:SI 105 [ D.98 ])))
381 | (clobber (reg:CC 17 flags))
382 | ]) -1
383 | (expr_list:REG_EQUAL (mult:SI (reg:SI 105 [ D.98 ])
384 | (const_int -1 [0xffffffffffffffff]))
385 | (nil)))
386 | (insn 37 36 38 7 (parallel [
387 | (set (reg:SI 108)
388 | (ashift:SI (reg:SI 107)
389 | (const_int 1 [0x1])))
390 | (clobber (reg:CC 17 flags))
391 | ]) -1
392 | (nil))
393 | (insn 38 37 39 7 (set (reg:SI 107)
394 | (reg:SI 108)) -1
395 | (expr_list:REG_EQUAL (mult:SI (reg:SI 105 [ D.98 ])
396 | (const_int -2 [0xfffffffffffffffe]))
397 | (nil)))
398 | (insn 39 38 40 7 (set (reg:SI 97 [ D.99 ])
399 | (reg:SI 107)) -1
400 | (nil))
401 | (insn 40 39 53 7 (parallel [
402 | (set (reg/v:SI 102 [ input ])
403 | (plus:SI (reg:SI 89 [ D.99 ])
404 | (reg:SI 97 [ D.99 ])))
405 | (clobber (reg:CC 17 flags))
406 | ]) -1
407 | (nil))
408 | (jump_insn 53 40 54 7 (set (pc)
409 | (label_ref 41)) -1
410 | (nil)
411 | -> 41)
412 | (barrier 54 53 56)
413 | (code_label 56 54 55 8 5 "" [1 uses])
414 | (note 55 56 11 8 [bb 8] NOTE_INSN_BASIC_BLOCK)
415 | (insn 11 55 41 8 (set (reg:SI 94 [ D.99 ])
416 | (const_int 0 [0])) -1
417 | (nil))
418 | (code_label 41 11 42 9 2 ("instr23") [1 uses])
419 | (note 42 41 43 9 [bb 9] NOTE_INSN_BASIC_BLOCK)
420 | (insn 43 42 44 9 (parallel [
421 | (set (reg:SI 109 [ D.99 ])
422 | (plus:SI (reg:SI 94 [ D.99 ])
423 | (reg/v:SI 102 [ input ])))
424 | (clobber (reg:CC 17 flags))
425 | ]) -1
426 | (nil))
427 | (insn 44 43 48 9 (set (reg:SI 101 [ ])
428 | (reg:SI 109 [ D.99 ])) -1
429 | (nil))
430 | (insn 48 44 51 9 (set (reg/i:SI 0 ax)
431 | (reg:SI 101 [ ])) -1
432 | (nil))
433 | (insn 51 48 0 9 (use (reg/i:SI 0 ax)) -1
434 | (nil))
435 |
436 | This goes through numerous optimizations and transformations (e.g. register
437 | allocation, instruction selection), before reaching code.
438 |
439 | Currently libgccjit.so goes through an intermediate stage of writing
440 | its generate code to disk as assembler, and then converting that to
441 | machine code. This generated assembler can be seen by setting::
442 |
443 | gcc_jit_context_set_bool_option (ctxt,
444 | GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES,
445 | 1);
446 |
447 | and reviewing the files::
448 |
449 | .file "fake.c"
450 | .text
451 | .p2align 4,,15
452 | .globl fibonacci
453 | .type fibonacci, @function
454 | fibonacci:
455 | .LFB0:
456 | .cfi_startproc
457 | cmpl $1, %edi
458 | pushq %r12
459 | .cfi_def_cfa_offset 16
460 | .cfi_offset 12, -16
461 | movl %edi, %r12d
462 | pushq %rbp
463 | .cfi_def_cfa_offset 24
464 | .cfi_offset 6, -24
465 | pushq %rbx
466 | .cfi_def_cfa_offset 32
467 | .cfi_offset 3, -32
468 | jle .L5
469 | movl %edi, %ebx
470 | xorl %ebp, %ebp
471 | .p2align 4,,10
472 | .p2align 3
473 | .L3:
474 | leal -1(%rbx), %edi
475 | subl $2, %ebx
476 | call fibonacci@PLT
477 | addl %eax, %ebp
478 | cmpl $1, %ebx
479 | jg .L3
480 | andl $1, %r12d
481 | .L2:
482 | leal 0(%rbp,%r12), %eax
483 | popq %rbx
484 | .cfi_remember_state
485 | .cfi_def_cfa_offset 24
486 | popq %rbp
487 | .cfi_def_cfa_offset 16
488 | popq %r12
489 | .cfi_def_cfa_offset 8
490 | ret
491 | .L5:
492 | .cfi_restore_state
493 | xorl %ebp, %ebp
494 | jmp .L2
495 | .cfi_endproc
496 | .LFE0:
497 | .size fibonacci, .-fibonacci
498 | .ident "GCC: (GNU) 4.9.0 20131004 (experimental)"
499 | .section .note.GNU-stack,"",@progbits
500 |
501 | This code is then injected into the process, and run (and calculates the
502 | correct result!).
503 |
504 | It's possible to set up "source code" locations for the bytecodes. In our
505 | test example we do this in a rather contrived way by associating the
506 | ``stackvm`` bytecodes with the locations in ``main.cc`` containing the
507 | table initializing them, but in a real interpreter you'd get this data
508 | from the parser.
509 |
510 | This source location data is carried through into the ``regvm`` code,
511 | then into the ``libgccjit.so`` represention, and into the
512 | JIT-compiled code. By enabling::
513 |
514 | gcc_jit_context_set_bool_option (ctxt,
515 | GCC_JIT_BOOL_OPTION_DEBUGINFO,
516 | 1);
517 |
518 | and turning off optimizations with::
519 |
520 | gcc_jit_context_set_int_option (ctxt,
521 | GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL,
522 | 0);
523 |
524 | it's possible to step through the individual bytecodes in a debugger::
525 |
526 | (gdb) break fibonacci
527 | (gdb) run
528 | Breakpoint 1, fibonacci (input=8) at main.cc:43
529 | 43 DUP,
530 | (gdb) list
531 | 38 const int FIRST_LINE = __LINE__ + 5;
532 | 39 const char fibonacci[] = {
533 | 40 // stack: [arg]
534 | 41
535 | 42 // 0:
536 | 43 DUP,
537 | 44 // stack: [arg, arg]
538 | 45
539 | 46 // 1:
540 | 47 PUSH_INT_CONST, 2,
541 | (gdb) next
542 | 47 PUSH_INT_CONST, 2,
543 | (gdb) next
544 | 51 BINARY_INT_COMPARE_LT,
545 | (gdb) next
546 | 55 JUMP_ABS_IF_TRUE, 17,
547 | (gdb) next
548 | 59 DUP,
549 | (gdb) next
550 | 63 PUSH_INT_CONST, 1,
551 | (gdb) next
552 | 67 BINARY_INT_SUBTRACT,
553 | (gdb) next
554 | 71 CALL_INT,
555 | (gdb) next
556 | Breakpoint 1, fibonacci (input=7) at main.cc:43
557 | 43 DUP,
558 | (...etc)
559 |
560 | It's also possible to step through with optimizations enabled, but
561 | execution appears to skip around the bytecodes in a manner typical
562 | for an optimizing compiler.
563 |
--------------------------------------------------------------------------------
/location.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2013 David Malcolm
3 | Copyright 2013 Red Hat, Inc.
4 |
5 | This is free software: you can redistribute it and/or modify it
6 | under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful, but
11 | WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 | General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see
17 | .
18 | */
19 |
20 | #ifndef LOCATION_H
21 | #define LOCATION_H
22 |
23 | /* Location within source code, for use in debuginfo. */
24 | struct location
25 | {
26 | const char *m_filename;
27 | int m_linenum;
28 | int m_colnum;
29 | };
30 |
31 | #endif
32 |
--------------------------------------------------------------------------------
/main.cc:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2013 David Malcolm
3 | Copyright 2013 Red Hat, Inc.
4 |
5 | This is free software: you can redistribute it and/or modify it
6 | under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful, but
11 | WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 | General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see
17 | .
18 | */
19 |
20 | #include
21 | #include
22 |
23 | #include "stackvm.h"
24 | #include "regvm.h"
25 |
26 | /*
27 | Simple recursive fibonacci implementation, roughly equivalent to:
28 |
29 | int fibonacci(int arg)
30 | {
31 | if (arg < 2) {
32 | return arg
33 | }
34 | return fibonacci(arg-1) + fibonacci(arg-2)
35 | }
36 | */
37 | using namespace stackvm;
38 | const int FIRST_LINE = __LINE__ + 5;
39 | const char fibonacci[] = {
40 | // stack: [arg]
41 |
42 | // 0:
43 | DUP,
44 | // stack: [arg, arg]
45 |
46 | // 1:
47 | PUSH_INT_CONST, 2,
48 | // stack: [arg, arg, 2]
49 |
50 | // 3:
51 | BINARY_INT_COMPARE_LT,
52 | // stack: [arg, (arg < 2)]
53 |
54 | // 4:
55 | JUMP_ABS_IF_TRUE, 17,
56 | // stack: [arg]
57 |
58 | // 6:
59 | DUP,
60 | // stack: [arg, arg]
61 |
62 | // 7:
63 | PUSH_INT_CONST, 1,
64 | // stack: [arg, arg, 1]
65 |
66 | // 9:
67 | BINARY_INT_SUBTRACT,
68 | // stack: [arg, (arg - 1)
69 |
70 | // 10:
71 | CALL_INT,
72 | // stack: [arg, fib(arg - 1)]
73 |
74 | // 11:
75 | ROT,
76 | // stack: [fib(arg - 1), arg]
77 |
78 | // 12:
79 | PUSH_INT_CONST, 2,
80 | // stack: [fib(arg - 1), arg, 2]
81 |
82 | // 14:
83 | BINARY_INT_SUBTRACT,
84 | // stack: [fib(arg - 1), arg, (arg - 2)
85 |
86 | // 15:
87 | CALL_INT,
88 | // stack: [fib(arg - 1), fib(arg - 1)]
89 |
90 | // 16:
91 | BINARY_INT_ADD,
92 | // stack: [fib(arg - 1) + fib(arg - 1)]
93 |
94 | // 17:
95 | RETURN_INT
96 | };
97 |
98 | typedef int (*compiled_code) (int);
99 |
100 | int main(int argc, const char **argv)
101 | {
102 | stackvm::bytecode * scode = new stackvm::bytecode(fibonacci,
103 | sizeof(fibonacci));
104 |
105 | /* Set up line-numbering in bytecode to point into the array
106 | initializer above, so that stepping through the JIT-generated
107 | code in the debugger will step through the above.
108 | We do it "by hand" here; in a real interpreter you'd presumably set
109 | this up with information from your parser. */
110 | scode->set_location(0, __FILE__, FIRST_LINE + 0, 2);
111 | scode->set_location(1, __FILE__, FIRST_LINE + 4, 2);
112 | scode->set_location(3, __FILE__, FIRST_LINE + 8, 2);
113 | scode->set_location(4, __FILE__, FIRST_LINE + 12, 2);
114 | scode->set_location(6, __FILE__, FIRST_LINE + 16, 2);
115 | scode->set_location(7, __FILE__, FIRST_LINE + 20, 2);
116 | scode->set_location(9, __FILE__, FIRST_LINE + 24, 2);
117 | scode->set_location(10, __FILE__, FIRST_LINE + 28, 2);
118 | scode->set_location(11, __FILE__, FIRST_LINE + 32, 2);
119 | scode->set_location(12, __FILE__, FIRST_LINE + 36, 2);
120 | scode->set_location(14, __FILE__, FIRST_LINE + 40, 2);
121 | scode->set_location(15, __FILE__, FIRST_LINE + 44, 2);
122 | scode->set_location(16, __FILE__, FIRST_LINE + 48, 2);
123 | scode->set_location(17, __FILE__, FIRST_LINE + 52, 2);
124 |
125 | scode->disassemble(stdout);
126 |
127 | stackvm::vm *sv = new stackvm::vm(scode);
128 | printf("sv->interpret(8) = %i\n", sv->interpret(8));
129 |
130 | regvm::wordcode * regcode = scode->compile_to_regvm();
131 | regcode->disassemble(stdout);
132 |
133 | regvm::vm *rv = new regvm::vm(regcode);
134 | printf("rv->interpret(8) = %i\n", rv->interpret(8));
135 |
136 | compiled_code code = (compiled_code)regcode->compile();
137 | printf("code (8) = %i\n", code (8));
138 | }
139 |
--------------------------------------------------------------------------------
/regvm.cc:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2013 David Malcolm
3 | Copyright 2013 Red Hat, Inc.
4 |
5 | This is free software: you can redistribute it and/or modify it
6 | under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful, but
11 | WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 | General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see
17 | .
18 | */
19 |
20 | #include
21 | #include
22 |
23 | #include "regvm.h"
24 | #include "libgccjit.h"
25 |
26 | using namespace regvm;
27 |
28 | static const int num_inputs[NUM_OPCODES] = {
29 | 1, // COPY_INT,
30 | 2, // BINARY_INT_ADD,
31 | 2, // BINARY_INT_SUBTRACT,
32 | 2, // BINARY_INT_COMPARE_LT,
33 | 2, // JUMP_ABS_IF_TRUE,
34 | 1, // CALL_INT,
35 | 1, // RETURN_INT,
36 | };
37 |
38 | instr::instr(enum opcode op, int output_reg, input a, const location &loc)
39 | : m_op(op),
40 | m_output_reg(output_reg),
41 | m_inputA(a),
42 | m_inputB(CONSTANT, 0),
43 | m_loc(loc)
44 | {
45 | assert(num_inputs[op] == 1);
46 | }
47 |
48 |
49 | instr::instr(enum opcode op, int output_reg, input lhs, input rhs, const location &loc)
50 | : m_op(op),
51 | m_output_reg(output_reg),
52 | m_inputA(lhs),
53 | m_inputB(rhs),
54 | m_loc(loc)
55 | {
56 | assert(num_inputs[op] == 2);
57 | }
58 |
59 | static void
60 | write_assign_to_lhs(FILE *out, int output_reg)
61 | {
62 | fprintf(out, "R%i = ", output_reg);
63 | }
64 |
65 | static void
66 | write_rvalue(FILE *out, input in)
67 | {
68 | switch (in.m_addrmode) {
69 | case CONSTANT:
70 | fprintf(out, "%i", in.m_value);
71 | break;
72 |
73 | case REGISTER:
74 | fprintf(out, "R%i", in.m_value);
75 | break;
76 |
77 | default:
78 | assert(0);
79 | }
80 | }
81 |
82 | static void
83 | write_any_loc(FILE *out, const instr &ins)
84 | {
85 | const location &loc = ins.m_loc;
86 | if (loc.m_filename) {
87 | fprintf(out, " /* %s:%i:%i */", loc.m_filename, loc.m_linenum, loc.m_colnum);
88 | }
89 | }
90 |
91 | static void
92 | write_binary_op(FILE *out, const instr &ins, const char *sym)
93 | {
94 | write_assign_to_lhs(out, ins.m_output_reg);
95 | write_rvalue(out, ins.m_inputA);
96 | fprintf(out, " %s ", sym);
97 | write_rvalue(out, ins.m_inputB);
98 | fprintf(out, ";");
99 | write_any_loc(out, ins);
100 | fprintf(out, "\n");
101 | }
102 |
103 | void instr::disassemble(FILE *out) const
104 | {
105 | switch (m_op) {
106 | case COPY_INT:
107 | write_assign_to_lhs(out, m_output_reg);
108 | write_rvalue(out, m_inputA);
109 | fprintf(out, ";");
110 | write_any_loc(out, *this);
111 | fprintf(out, "\n");
112 | break;
113 |
114 | case BINARY_INT_ADD:
115 | write_binary_op(out, *this, "+");
116 | break;
117 |
118 | case BINARY_INT_SUBTRACT:
119 | write_binary_op(out, *this, "-");
120 | break;
121 |
122 | case BINARY_INT_COMPARE_LT:
123 | write_binary_op(out, *this, "<");
124 | break;
125 |
126 | case JUMP_ABS_IF_TRUE:
127 | fprintf(out, "IF (");
128 | write_rvalue(out, m_inputA);
129 | fprintf(out, ") GOTO ");
130 | write_rvalue(out, m_inputB);
131 | fprintf(out, ";");
132 | write_any_loc(out, *this);
133 | fprintf(out, "\n");
134 | break;
135 |
136 | case CALL_INT:
137 | write_assign_to_lhs(out, m_output_reg);
138 | fprintf(out, "CALL(");
139 | write_rvalue(out, m_inputA);
140 | fprintf(out, ");");
141 | write_any_loc(out, *this);
142 | fprintf(out, "\n");
143 | break;
144 |
145 | case RETURN_INT:
146 | fprintf(out, "RETURN(");
147 | write_rvalue(out, m_inputA);
148 | fprintf(out, ");");
149 | write_any_loc(out, *this);
150 | fprintf(out, "\n");
151 | break;
152 |
153 | default:
154 | assert(0); // FIXME
155 | }
156 | }
157 |
158 | void wordcode::disassemble(FILE *out) const
159 | {
160 | for (int pc = 0; pc < (int)m_instrs.size(); /* */) {
161 | disassemble_at(out, pc);
162 | }
163 | }
164 | void wordcode::disassemble_at(FILE *out, int &pc) const
165 | {
166 | fprintf(out, "[%i] : ", pc);
167 | const instr& ins = fetch_instr(pc);
168 | ins.disassemble(out);
169 | }
170 |
171 | const instr&
172 | wordcode::fetch_instr(int &pc) const
173 | {
174 | return m_instrs[pc++];
175 | }
176 |
177 | // Experimental JIT compilation via libgccjit:
178 | #if 1
179 | static gcc_jit_location *
180 | make_jit_loc(gcc_jit_context *ctxt, const struct location &loc)
181 | {
182 | if (!loc.m_filename) {
183 | return NULL;
184 | }
185 | return gcc_jit_context_new_location (ctxt,
186 | loc.m_filename,
187 | loc.m_linenum,
188 | loc.m_colnum);
189 | }
190 |
191 | class frame_compiler
192 | {
193 | public:
194 | frame_compiler(gcc_jit_context *ctxt,
195 | gcc_jit_function *fn,
196 | gcc_jit_location *fn_loc) :
197 | m_ctxt(ctxt),
198 | m_fn(fn),
199 | m_int_type(gcc_jit_context_get_type (m_ctxt, GCC_JIT_TYPE_INT))
200 | {
201 | for (int i = 0; i < NUM_REGISTERS; i++) {
202 | char buf[10];
203 | sprintf (buf, "R%i", i);
204 | gcc_jit_lvalue *local =
205 | gcc_jit_function_new_local (fn,
206 | fn_loc,
207 | m_int_type,
208 | buf);
209 | m_locals.push_back(local);
210 | }
211 | }
212 |
213 | // We will have one local per "register":
214 | std::vector m_locals;
215 |
216 | gcc_jit_rvalue *eval_int(const input& in) const;
217 | gcc_jit_lvalue *get_reg(int idx) const;
218 | gcc_jit_lvalue *get_output_reg(const instr &ins) const;
219 |
220 | private:
221 | gcc_jit_context *m_ctxt;
222 | gcc_jit_function *m_fn;
223 | gcc_jit_type *m_int_type;
224 | };
225 |
226 |
227 | gcc_jit_rvalue *
228 | frame_compiler::eval_int(const input& in) const
229 | {
230 | switch (in.m_addrmode) {
231 | case CONSTANT:
232 | return
233 | gcc_jit_context_new_rvalue_from_int (m_ctxt,
234 | m_int_type,
235 | in.m_value);
236 | case REGISTER:
237 | assert(in.m_value >= 0);
238 | assert(in.m_value < NUM_REGISTERS);
239 | return gcc_jit_lvalue_as_rvalue (m_locals[in.m_value]);
240 |
241 | default:
242 | assert(0);
243 | }
244 | }
245 |
246 | gcc_jit_lvalue *
247 | frame_compiler::get_reg(int idx) const
248 | {
249 | return m_locals[idx];
250 | }
251 |
252 | gcc_jit_lvalue *
253 | frame_compiler::get_output_reg(const instr &ins) const
254 | {
255 | return get_reg (ins.m_output_reg);
256 | }
257 |
258 | void *wordcode::compile()
259 | {
260 | gcc_jit_context *ctxt = gcc_jit_context_acquire ();
261 |
262 | gcc_jit_context_set_bool_option (ctxt,
263 | GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE,
264 | 1);
265 | gcc_jit_context_set_bool_option (ctxt,
266 | GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE,
267 | 1);
268 | gcc_jit_context_set_int_option (ctxt,
269 | GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL,
270 | 3);
271 | gcc_jit_context_set_bool_option (ctxt,
272 | GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES,
273 | 1);
274 | gcc_jit_context_set_bool_option (ctxt,
275 | GCC_JIT_BOOL_OPTION_DUMP_EVERYTHING,
276 | 1);
277 |
278 | int pc;
279 |
280 | gcc_jit_location *fn_loc = make_jit_loc(ctxt, m_instrs[0].m_loc);
281 |
282 | gcc_jit_type *int_type =
283 | gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
284 | gcc_jit_type *bool_type =
285 | gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_BOOL);
286 | gcc_jit_param *param =
287 | gcc_jit_context_new_param (ctxt, fn_loc, int_type, "input");
288 | gcc_jit_function *fn =
289 | gcc_jit_context_new_function (ctxt,
290 | make_jit_loc(ctxt, m_instrs[0].m_loc),
291 | GCC_JIT_FUNCTION_EXPORTED,
292 | int_type,
293 | "fibonacci", /* FIXME */
294 | 1, ¶m, 0);
295 | frame_compiler f(ctxt, fn, fn_loc);
296 |
297 | gcc_jit_block *initial = gcc_jit_function_new_block (fn, "initial");
298 |
299 | // 1st pass: create blocks, one per opcode:
300 | std::vector blocks;
301 | pc = 0;
302 | for (std::vector::iterator it = m_instrs.begin();
303 | it != m_instrs.end();
304 | ++it, ++pc)
305 | {
306 | char buf[16];
307 | sprintf (buf, "instr%i", pc);
308 | gcc_jit_block *block = gcc_jit_function_new_block (fn, buf);
309 | blocks.push_back(block);
310 | }
311 |
312 | // Assign param to R0:
313 | gcc_jit_block_add_assignment (initial,
314 | make_jit_loc(ctxt, m_instrs[0].m_loc),
315 | f.get_reg (0),
316 | gcc_jit_param_as_rvalue (param));
317 | // ...and jump to insn 0
318 | gcc_jit_block_end_with_jump (initial, NULL, blocks[0]);
319 |
320 | // 2nd pass: fill in instructions:
321 | for (pc = 0; pc < (int)m_instrs.size(); pc++)
322 | {
323 | gcc_jit_location *loc = make_jit_loc(ctxt, m_instrs[pc].m_loc);
324 | gcc_jit_block *block = blocks[pc];
325 | gcc_jit_block *next_block = (pc < (int)m_instrs.size()) ? blocks[pc + 1] : NULL;
326 |
327 | const instr &ins = m_instrs[pc];
328 | ins.disassemble(stdout);
329 |
330 | switch (ins.m_op) {
331 | case COPY_INT:
332 | {
333 | gcc_jit_rvalue *src = f.eval_int(ins.m_inputA);
334 | gcc_jit_lvalue *dst = f.get_output_reg(ins);
335 | gcc_jit_block_add_assignment (block, loc, dst, src);
336 | gcc_jit_block_end_with_jump (block, loc, next_block);
337 | }
338 | break;
339 |
340 | case BINARY_INT_ADD:
341 | case BINARY_INT_SUBTRACT:
342 | {
343 | enum gcc_jit_binary_op op
344 | = ((ins.m_op == BINARY_INT_ADD)
345 | ? GCC_JIT_BINARY_OP_PLUS : GCC_JIT_BINARY_OP_MINUS);
346 | gcc_jit_rvalue *lhs = f.eval_int(ins.m_inputA);
347 | gcc_jit_rvalue *rhs = f.eval_int(ins.m_inputB);
348 | gcc_jit_lvalue *dst = f.get_output_reg(ins);
349 | gcc_jit_block_add_assignment (
350 | block, loc, dst,
351 | gcc_jit_context_new_binary_op (ctxt, loc, op,
352 | int_type,
353 | lhs, rhs));
354 | gcc_jit_block_end_with_jump (block, loc, next_block);
355 | }
356 | break;
357 |
358 | case BINARY_INT_COMPARE_LT:
359 | {
360 | gcc_jit_rvalue *lhs = f.eval_int(ins.m_inputA);
361 | gcc_jit_rvalue *rhs = f.eval_int(ins.m_inputB);
362 | gcc_jit_lvalue *dst = f.get_output_reg(ins);
363 | gcc_jit_block_add_assignment (
364 | block, loc, dst,
365 | gcc_jit_context_new_cast (
366 | ctxt, loc,
367 | gcc_jit_context_new_comparison (ctxt, loc, GCC_JIT_COMPARISON_LT,
368 | lhs, rhs),
369 | int_type));
370 | gcc_jit_block_end_with_jump (block, loc, next_block);
371 | }
372 | break;
373 |
374 | case JUMP_ABS_IF_TRUE:
375 | {
376 | gcc_jit_rvalue *int_flag = f.eval_int(ins.m_inputA);
377 | gcc_jit_rvalue *bool_flag =
378 | gcc_jit_context_new_cast (ctxt, loc, int_flag, bool_type);
379 |
380 | assert(ins.m_inputB.m_addrmode == CONSTANT);
381 | int dest = ins.m_inputB.m_value;
382 |
383 | gcc_jit_block *on_true = blocks[dest];
384 | gcc_jit_block *on_false = blocks[pc + 1];
385 |
386 | gcc_jit_block_end_with_conditional (block, loc,
387 | bool_flag,
388 | on_true,
389 | on_false);
390 | }
391 | break;
392 |
393 | case CALL_INT:
394 | {
395 | gcc_jit_rvalue *arg = f.eval_int(ins.m_inputA);
396 | gcc_jit_lvalue *dst = f.get_output_reg(ins);
397 | gcc_jit_block_add_assignment (
398 | block, loc, dst,
399 | gcc_jit_context_new_call (ctxt, loc, fn,
400 | 1, &arg));
401 | gcc_jit_block_end_with_jump (block, loc, next_block);
402 | }
403 | break;
404 |
405 | case RETURN_INT:
406 | {
407 | gcc_jit_rvalue *result = f.eval_int(ins.m_inputA);
408 | gcc_jit_block_end_with_return (block, loc, result);
409 | }
410 | break;
411 |
412 | default:
413 | assert(0); // FIXME
414 | }
415 | }
416 |
417 | gcc_jit_result *result = gcc_jit_context_compile (ctxt);
418 | gcc_jit_context_release (ctxt);
419 |
420 | return gcc_jit_result_get_code (result,
421 | "fibonacci" /* FIXME */);
422 | /* FIXME: this leaks "result" */
423 | }
424 | #endif
425 |
426 | int vm::interpret(int input)
427 | {
428 | frame f;
429 | int pc = 0;
430 | debug_begin_frame(input);
431 | f.set_int_reg(0, input);
432 | while (1) {
433 | debug_begin_opcode(f, pc);
434 | const instr &ins = m_wordcode->fetch_instr(pc);
435 | switch (ins.m_op) {
436 | case COPY_INT:
437 | {
438 | int result = f.eval_int(ins.m_inputA);
439 | f.set_int_reg(ins.m_output_reg, result);
440 | }
441 | break;
442 |
443 | case BINARY_INT_ADD:
444 | {
445 | int lhs = f.eval_int(ins.m_inputA);
446 | int rhs = f.eval_int(ins.m_inputB);
447 | int result = lhs + rhs;
448 | f.set_int_reg(ins.m_output_reg, result);
449 | }
450 | break;
451 |
452 | case BINARY_INT_SUBTRACT:
453 | {
454 | int lhs = f.eval_int(ins.m_inputA);
455 | int rhs = f.eval_int(ins.m_inputB);
456 | int result = lhs - rhs;
457 | f.set_int_reg(ins.m_output_reg, result);
458 | }
459 | break;
460 |
461 | case BINARY_INT_COMPARE_LT:
462 | {
463 | int lhs = f.eval_int(ins.m_inputA);
464 | int rhs = f.eval_int(ins.m_inputB);
465 | bool result = lhs < rhs;
466 | f.set_bool_reg(ins.m_output_reg, result);
467 | }
468 | break;
469 |
470 | case JUMP_ABS_IF_TRUE:
471 | {
472 | bool flag = f.eval_int(ins.m_inputA);
473 | int dest = f.eval_int(ins.m_inputB);
474 | if (flag) {
475 | pc = dest;
476 | }
477 | }
478 | break;
479 |
480 | case CALL_INT:
481 | {
482 | int arg = f.eval_int(ins.m_inputA);
483 | int result = interpret(arg); //recurse
484 | f.set_int_reg(ins.m_output_reg, result);
485 | }
486 | break;
487 |
488 | case RETURN_INT:
489 | {
490 | int result = f.eval_int(ins.m_inputA);
491 | debug_end_frame(pc, result);
492 | return result;
493 | }
494 | break;
495 |
496 | default:
497 | assert(0); // FIXME
498 | }
499 | debug_end_opcode(pc);
500 | }
501 | }
502 |
503 | frame::frame()
504 | {
505 | for (int i = 0; i < NUM_REGISTERS; i++) {
506 | m_registers[i] = 0xDEADBEEF;
507 | }
508 | }
509 |
510 | int frame::eval_int(const input& in) const
511 | {
512 | switch (in.m_addrmode) {
513 | case CONSTANT:
514 | return in.m_value;
515 | case REGISTER:
516 | assert(in.m_value >= 0);
517 | assert(in.m_value < NUM_REGISTERS);
518 | return m_registers[in.m_value];
519 | default:
520 | assert(0);
521 | }
522 | }
523 |
524 | int frame::get_int_reg(int idx)
525 | {
526 | assert(idx >= 0);
527 | assert(idx < NUM_REGISTERS);
528 | printf("setting reg%i as %i\n", idx, m_registers[idx]);
529 | return m_registers[idx];
530 | }
531 |
532 | void frame::set_int_reg(int idx, int val)
533 | {
534 | assert(idx >= 0);
535 | assert(idx < NUM_REGISTERS);
536 | printf("setting reg%i to %i\n", idx, val);
537 | m_registers[idx] = val;
538 | }
539 |
540 | void frame::debug_registers(FILE *out) const
541 | {
542 | for (int i = 0; i < NUM_REGISTERS; i++) {
543 | fprintf(out, " register %i: %i\n", i, m_registers[i]);
544 | }
545 | }
546 |
547 | void vm::debug_begin_frame(int arg)
548 | {
549 | printf("BEGIN FRAME: arg=%i\n", arg);
550 | }
551 |
552 | void vm::debug_end_frame(int pc, int result)
553 | {
554 | printf("END FRAME: result=%i\n", result);
555 | }
556 |
557 | void vm::debug_begin_opcode(const frame &f, int pc)
558 | {
559 | printf("begin opcode: ");
560 | m_wordcode->disassemble_at(stdout, pc);
561 | printf(" registers: \n");
562 | f.debug_registers(stdout);
563 | }
564 |
565 | void vm::debug_end_opcode(int pc)
566 | {
567 | }
568 |
--------------------------------------------------------------------------------
/regvm.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2013 David Malcolm
3 | Copyright 2013 Red Hat, Inc.
4 |
5 | This is free software: you can redistribute it and/or modify it
6 | under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful, but
11 | WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 | General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see
17 | .
18 | */
19 |
20 | #include
21 |
22 | #include "location.h"
23 |
24 | struct gcc_jit_context;
25 |
26 | namespace regvm {
27 |
28 | const int NUM_REGISTERS = 4;
29 |
30 | // A simple register-based virtual machine
31 | enum addrmode {
32 | CONSTANT,
33 | REGISTER,
34 | };
35 |
36 | enum opcode {
37 | COPY_INT,
38 | BINARY_INT_ADD,
39 | BINARY_INT_SUBTRACT,
40 | BINARY_INT_COMPARE_LT,
41 | JUMP_ABS_IF_TRUE,
42 | CALL_INT,
43 | RETURN_INT,
44 |
45 | NUM_OPCODES,
46 | };
47 |
48 | struct input
49 | {
50 | input(enum addrmode addrmode, int value)
51 | : m_addrmode(addrmode),
52 | m_value(value)
53 | {}
54 |
55 | enum addrmode m_addrmode;
56 | int m_value;
57 | };
58 |
59 | struct instr
60 | {
61 | instr(enum opcode op, int output_reg, input a, const location &loc);
62 |
63 | instr(enum opcode op, int output_reg, input lhs, input rhs, const location &loc);
64 |
65 | void disassemble(FILE *out) const;
66 |
67 | enum opcode m_op;
68 | int m_output_reg;
69 | input m_inputA;
70 | input m_inputB;
71 | /* Source location: */
72 | location m_loc;
73 | };
74 |
75 | class wordcode
76 | {
77 | public:
78 | wordcode(std::vector instrs)
79 | : m_instrs(instrs)
80 | {}
81 |
82 | void disassemble(FILE *out) const;
83 |
84 | void disassemble_at(FILE *out, int &pc) const;
85 |
86 | const instr&
87 | fetch_instr(int &pc) const;
88 |
89 | int
90 | fetch_arg_int(int &pc) const;
91 |
92 | void *compile();
93 |
94 | private:
95 | std::vector m_instrs;
96 | };
97 |
98 | class frame
99 | {
100 | public:
101 | frame();
102 |
103 | int eval_int(const input& in) const;
104 | bool eval_bool(const input& in) const { return eval_int(in) != 0; }
105 |
106 | int get_int_reg(int idx);
107 | void set_int_reg(int idx, int val);
108 |
109 | bool get_bool_reg(int idx) { return get_int_reg(idx) != 0; }
110 | void set_bool_reg(int idx ,bool flag) { set_int_reg(idx, flag ? 1 : 0); }
111 |
112 | void debug_registers(FILE *out) const;
113 |
114 | private:
115 | int m_registers[NUM_REGISTERS];
116 | };
117 |
118 | class vm
119 | {
120 | public:
121 | vm(wordcode *code)
122 | : m_wordcode(code)
123 | {}
124 | ~vm() {}
125 |
126 | int interpret(int arg);
127 |
128 | private:
129 | void debug_begin_frame(int arg);
130 | void debug_end_frame(int pc, int result);
131 | void debug_begin_opcode(const frame &f, int pc);
132 | void debug_end_opcode(int pc);
133 |
134 | private:
135 | wordcode *m_wordcode;
136 | };
137 |
138 | }; // namespace regvm
139 |
--------------------------------------------------------------------------------
/stackvm.cc:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2013 David Malcolm
3 | Copyright 2013 Red Hat, Inc.
4 |
5 | This is free software: you can redistribute it and/or modify it
6 | under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful, but
11 | WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 | General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see
17 | .
18 | */
19 |
20 | #include
21 | #include
22 | #include
23 | #include