Dynamic Abstraction and Universal Executables

Think you want to provide some information to your user. You want to let your user know how is progress of your program process. You may target Xorg and use related functions and libraries to show a Window and render text on it. As you know this won’t work on Windows. You may install a 3rd-pary Xorg server on Windows, but it doesn’t work in the Windows’ native way. You may use Windows API to open a window and render your text, but it doesn’t run on any other platform. You may write a library that abstracts the process of creating a window and rendering a text and implement it for different platforms, then link the appropriate version for your target platform at compile-time. And this is the magical way that works natively on each platform. Let’s go deeper with another example. You may want to print your program progress or result to Console. Each platform provides one or more buffers/streams for Console I/O in different ways, but you just simple call printf function from C Standard Library. You don’t think about Linux Framebuffer Console, Terminal Emulators, VGA, or Windows API, You don’t need. You just use the Standard printf function. Your compiler, links your program to the standard C library installed on your system. it contains the appropriate implementation of printf for your OS. the magical things happens when your OS is executing your program and reachs to the part you are calling printf. Now, your OS should load the installed C library and call printf to load your program. This is called “Dynamic Linking”. Let’s go further. Each OS has its own API. the APIs may be different even in each versions of the same OS. But by abstracting this APIs, you can write OS-independent programs that runs on different platforms, without need to rewrite your program. The compiler uses your ‘Universal API’ function definations to compile your program, and produces binary appropriate for your OS. When you execute your program, the OS loads your program, and executes the main routine. Your program may calls print_message function from your Universal Library, then the OS will load the installed version of the library. library is compiled and installed before for the OS. It may use different functions from API of the OS and libraries like Xorg. When you call print_message, the OS starts to execute the Universal Library. the library may use Xorg the draw your message to screen, or use Windows API to write text to console, or print it directly to framebuffer of your graphic card. All of this dependent on which implementation of the library is installed on your OS. We can even go further, by implementing an OS-independent executable format. the Universal Executable Format is a kind of executable that can be executed on any operating systems, by use of the Universal Library. You just need to compile your program for some architectures you want, like x86, x86_64 and ARM, then it will work on any operating system running in these CPUs. It can be native code, that includes a new opcode for calling Universal Library. it would use index of function in the Universal Library, that’s constant on every platform, or being more verbose and portable, and use function names instead of index. We can also use an pseudo opcode, and compile it to architecture-depend opcode on installation or runtime, to make program more portable. Support for Universal Executables should built into operating system kernels to make it as fast as possible. It can be done by writing a kernel module or driver that loads into the operating system, or a JIT compiler that copies program opcodes to a executable area of memory, and converts Universal Library calls to native call of the OS. I would call this concept “Dynamic Abstraction”, as it’s a dynamic kind of abstraction of operating systems API. You just need a Universal Library for the target OS and a kernel module or JIT compiler. In this way, you can write programs that can natively run on every operating systems of an architecture, or write a operating system that’s compatible with programs from other operating systems.

Autogenerated by Jekyll.
This page is hosted here, Available to you under MIT License.
Copyright (C) 2018-2019 by Mohammad Amin Mollazadeh.
mail: madmanda2020@gmail.com