Compiler/virtual machine interpreter: Difference between revisions

m
→‎{{header|Phix}}: added syntax colouring, marked p2js incompatible
m (Update Zig to 0.9.0)
m (→‎{{header|Phix}}: added syntax colouring, marked p2js incompatible)
Line 2,725:
=={{header|Phix}}==
Reusing cgen.e from the [[Compiler/code_generator#Phix|Code Generator task]]
<!--<lang Phix>(notonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Compiler\vm.exw
-- ============================
--
-- Since we have generated executable machine code, the virtual machine, such as it is, is just
-- the higher level implementations of printc/i/s, see setbuiltins() in cgen.e
-- Otherwise the only difference between this and cgen.exw is call(code_mem) instead of decode().
--
-- A quick test (calculating fib(44) 10^6 times) suggests ~500 times faster than interp.exw -
-- which is to be expected given that a single add instruction (1 clock) here is implemented as
-- at least three (and quite possibly five!) resursive calls to interp() in the other.</span>
 
 
<span style="color: #000000;">format</span> <span style="color: #000000;">PE32</span>
format PE32
<span style="color: #000080;font-style:italic;">--format ELF32
-- Note: cgen generates 32-bit machine code, which cannot be executed directly from a 64-bit interpreter.
-- You can however, via the magic of either the above format directives, use a 64-bit version of
-- Phix to compile this (just add a -c command line option) to a 32-bit executable, which can.
-- It would not be particularly difficult to emit 32 or 64 bit code, but some source code files
-- would, fairly obviously, then be very nearly twice as long, and a fair bit harder to read.</span>
 
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (machine code!)</span>
include cgen.e
<span style="color: #008080;">include</span> <span style="color: #000000;">cgen</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
 
procedure main(sequence cl)
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">cl</span><span style="color: #0000FF;">)</span>
open_files(cl)
<span style="color: #000000;">open_files</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cl</span><span style="color: #0000FF;">)</span>
toks = lex()
<span style="color: #000000;">toks</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lex</span><span style="color: #0000FF;">()</span>
object t = parse()
<span style="color: #004080;">object</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">parse</span><span style="color: #0000FF;">()</span>
code_gen(t)
<span style="color: #000000;">code_gen</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
fixup()
<span style="color: #000000;">fixup</span><span style="color: #0000FF;">()</span>
if machine_bits()=32 then
<span style="color: #008080;">if</span> <span style="color: #7060A8;">machine_bits</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">32</span> <span style="color: #008080;">then</span>
-- ^ as per note above
<span style="color: #000080;font-style:italic;">-- ^ as per note above</span>
call(code_mem)
<span style="color: #000000;">call</span><span style="color: #0000FF;">(</span><span style="color: #000000;">code_mem</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
free({var_mem,code_mem})
<span style="color: #7060A8;">free</span><span style="color: #0000FF;">({</span><span style="color: #000000;">var_mem</span><span style="color: #0000FF;">,</span><span style="color: #000000;">code_mem</span><span style="color: #0000FF;">})</span>
close_files()
<span style="color: #000000;">close_files</span><span style="color: #0000FF;">()</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
 
--main(command_line())
<span style="color: #000080;font-style:italic;">--main(command_line())</span>
main({0,0,"count.c"})</lang>
<span style="color: #000000;">main</span><span style="color: #0000FF;">({</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"count.c"</span><span style="color: #0000FF;">})</span>
<!--</lang>-->
{{out}}
<pre>
7,794

edits