Memory layout of a data structure: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Typo.) |
m (→{{header|Phix}}: added syntax colouring the hard way) |
||
Line 749: | Line 749: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Phix does not support bit-fields directly. The nearest/sanest thing to do probably goes something like this (completely untested) |
Phix does not support bit-fields directly. The nearest/sanest thing to do probably goes something like this (completely untested) |
||
<lang Phix>constant CD=1, RD=2, TD=3, DTR=4, ... |
|||
<!--<lang Phix>--> |
|||
atom addr = allocate(2) -- or wherever |
|||
<span style="color: #008080;">constant</span> <span style="color: #000000;">CD</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">RD</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">TD</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">DTR</span><span style="color: #0000FF;">=</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">...</span> |
|||
⚫ | |||
<span style="color: #004080;">atom</span> <span style="color: #000000;">addr</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">allocate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- or wherever |
|||
sequence bits = int_to_bits(peek2u(addr),16) |
|||
⚫ | |||
integer dtr = bits[DTR] |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">bits</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">int_to_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">peek2u</span><span style="color: #0000FF;">(</span><span style="color: #000000;">addr</span><span style="color: #0000FF;">),</span><span style="color: #000000;">16</span><span style="color: #0000FF;">)</span> |
|||
--write |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">dtr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">bits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">DTR</span><span style="color: #0000FF;">]</span> |
|||
bits[DTR] = 1 |
|||
<span style="color: #000080;font-style:italic;">--write</span> |
|||
poke2(addr,bits_to_int(bits))</lang> |
|||
<span style="color: #000000;">bits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">DTR</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> |
|||
<span style="color: #7060A8;">poke2</span><span style="color: #0000FF;">(</span><span style="color: #000000;">addr</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">bits_to_int</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bits</span><span style="color: #0000FF;">))</span> |
|||
<!--</lang>--> |
|||
Naturally, you would be well advised to sequester such grubby details away in a small and separate unit/source code file (eg RS232.e) with a domain specific public API that does not leak implementation details (eg keep those constants private). There are 1/2/4/8 byte variants of peek and poke, and int-to-bits can extract anything from 1 to 53 bits on a 32-bit runtime, or up to 64 on a 64-bit runtime. |
Naturally, you would be well advised to sequester such grubby details away in a small and separate unit/source code file (eg RS232.e) with a domain specific public API that does not leak implementation details (eg keep those constants private). There are 1/2/4/8 byte variants of peek and poke, and int-to-bits can extract anything from 1 to 53 bits on a 32-bit runtime, or up to 64 on a 64-bit runtime. |
||
Alternatively you could use bit-masks, or it may be possible to enhance builtins/cffi.e to manage bit-fields, then again the above C entry does not exactly inspire confidence. |
Alternatively you could use bit-masks, or it may be possible to enhance builtins/cffi.e to manage bit-fields, then again the above C entry does not exactly inspire confidence. |