|
Machine Level Interface
We have grouped here a number of routines that you can use to access your machine at a low-level.
With this low-level machine interface you can read and write to memory.
You can also set up your own machine language routines and call them.
Some of the routines listed below are unsafe, in the sense that Phix cannot protect you if you use them incorrectly. You could crash your program or even your system.
Under Windows, the operating system will usually pop up a termination box giving a diagnostic message plus register information.
Under Linux you will typically get a segmentation violation. Note: To assist programmers in debugging code involving these unsafe routines, we have supplied [DEV (safe.e)]
DEV:
--DOCS: Sort out safe.e: add a constant to pAlloc.e of SAFETY=NORMAL/DYNAMIC/VALIDATE.
-- NORMAL: best performance, no checking
-- DYNAMIC: can be enabled per-application (via allocation_safety(bool active=false)
-- VALIDATE: always checked.
--
-- The argument to allocation_safety() can be true/false.
-- When SAFETY=NORMAL (as distributed) the compiler should not emit any extra code or runtime tests,
-- resulting in the best possible performance, and will ignore any calls to alloction_safety().
-- Note that Phix does not have the safe.e (of Euphoria, to replace machine.e),
-- largely because of the auto-include system, so this is the replacement.
- safe.e, an alternative to machine.e. If you copy euphoria\include\safe.e into the directory containing your program, and you rename safe.e as machine.e in that directory, your program will run using safer (but slower) versions of these low-level routines. safe.e can catch many errors, such as poking into a bad memory location. See the comments at the top of safe.e for complete instructions on how to use it. When using a package such as Win32Lib that does not use the allocate() function, you can only make limited use of safe.e**. These machine-level-interface routines are important because they allow Phix programmers to access low-level features of the hardware and operating system.
For some applications this is essential.
Machine code routines can be written by hand, or taken from the disassembled output of a compiler for C or some other language. There are many examples of inline assembly needed for the Phix runtime, perhaps the simplest of which is builtins\VM\pLen.e which implements the length() function. Remember that your machine code will be running in 32-bit protected mode. See (DEV) demo\dos32\callmach.ex for an example.
|