Host introspection: Difference between revisions

Content added Content deleted
(Added solution for Action!)
m (→‎{{header|Phix}}: added syntax colouring, made p2js compatible)
Line 931: Line 931:
=={{header|Phix}}==
=={{header|Phix}}==
Note that machine_word() and machine_bits() test the interpreter or compiled executable, rather than the OS or hardware.<br>
Note that machine_word() and machine_bits() test the interpreter or compiled executable, rather than the OS or hardware.<br>
Also, all known implementations of Phix are currently little-endian. See also platform(), which yields WINDOWS or LINUX.
Also, all known implementations of Phix are currently little-endian. See also platform(), which yields WINDOWS/LINUX/JS.
<lang Phix>function endianness()
<!--<lang Phix>(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
atom m4 = allocate(4)
<span style="color: #008080;">function</span> <span style="color: #000000;">endianness</span><span style="color: #0000FF;">()</span>
poke4(m4,#01020304)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
integer b1 = peek1s(m4)
<span style="color: #008080;">return</span> <span style="color: #008000;">"n/a (web browser)"</span>
free(m4)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if b1=#01 then
<span style="color: #004080;">atom</span> <span style="color: #000000;">m4</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">allocate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span>
return "big-endian"
<span style="color: #7060A8;">poke4</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#01020304</span><span style="color: #0000FF;">)</span>
elsif b1=#04 then
<span style="color: #004080;">integer</span> <span style="color: #000000;">b1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">peek1s</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m4</span><span style="color: #0000FF;">)</span>
return "little-endian"
<span style="color: #7060A8;">free</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m4</span><span style="color: #0000FF;">)</span>
else
<span style="color: #008080;">if</span> <span style="color: #000000;">b1</span><span style="color: #0000FF;">=</span><span style="color: #000000;">#01</span> <span style="color: #008080;">then</span>
return "???"
<span style="color: #008080;">return</span> <span style="color: #008000;">"big-endian"</span>
end if
<span style="color: #008080;">elsif</span> <span style="color: #000000;">b1</span><span style="color: #0000FF;">=</span><span style="color: #000000;">#04</span> <span style="color: #008080;">then</span>
end function
<span style="color: #008080;">return</span> <span style="color: #008000;">"little-endian"</span>

<span style="color: #008080;">else</span>
printf(1,"Endianness: %s\n",{endianness()})
<span style="color: #008080;">return</span> <span style="color: #008000;">"???"</span>
printf(1,"Word size: %d bytes/%d bits\n",{machine_word(),machine_bits()})</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Endianness: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">endianness</span><span style="color: #0000FF;">()})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Word size: %d bytes/%d bits\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">machine_word</span><span style="color: #0000FF;">(),</span><span style="color: #7060A8;">machine_bits</span><span style="color: #0000FF;">()})</span>
<!--</lang>-->
{{out}}
{{out}}
<pre>
<pre>
Line 957: Line 963:
Endianness: little-endian
Endianness: little-endian
Word size: 8 bytes/64 bits
Word size: 8 bytes/64 bits
</pre>
or
<pre>
Endianness: n/a (web browser)
Word size: 4 bytes/32 bits
</pre>
</pre>