Add a workflow that adds Neo-Desktop as an assignee when an issue is opened. Claude-Session: https://claude.ai/code/session_01B82eRQqxennD6yGZo4WkoY Co-authored-by: Claude <noreply@anthropic.com>
OS/2 SDK for clang-retro
C programs for OS/2 with clang-retro's clang, picolibc and JWlink:
| Target | Triple | Output | Runs on |
|---|---|---|---|
| 16-bit OS/2 1.x | i8086-pc-os2 |
NE program or DLL | OS/2 1.x, and 2.x to 4.x and ArcaOS |
| 32-bit OS/2 | i386-pc-os2 |
LX program or DLL | OS/2 2.0 to 4.x, eComStation, ArcaOS |
clang --target=i386-pc-os2 -Os hello.c -o hello.exe # 32-bit
clang --target=i386-pc-os2 -mwindows -Os pmhello.c -o pmhello.exe # 32-bit PM
clang --target=i386-pc-os2 -shared -Os mylib.c -o MYLIB.DLL # 32-bit DLL
clang --target=i8086-pc-os2 -Os hello.c -o hello.exe # 16-bit, small model
clang --target=i8086-pc-os2 -mcmodel=large -Os app.c -o app.exe # 16-bit, large model
clang --target=i8086-pc-os2 -shared -Os mylib.c -o MYLIB.DLL # 16-bit DLL
python3 tools/os2-imports.py MYLIB.DLL > mylib.lnk # its imports
clang --target=i8086-pc-os2 -Os app.c -Wl,@mylib.lnk -o app.exe # a program that uses it
What's here
include/os21x/: the 16-bit OS/2 1.x API (os2.h): types, the Dos calls, Vio, Kbd and Mou, the error codes, and the OS/2 1.x Presentation Manager: windows, messages, frames, menus, dialogs, the controls, the clipboard and pointers (pmwin.h), presentation spaces, fonts and bitmaps (pmgpi.h), and the switch list and profiles (pmshl.h). Every function is__far __pascalwith far pointers, as in the IBM and Microsoft toolkits. A 1.x message is aUSHORTand its coordinates areSHORT, and the structures are the 1.x ones (QMSGis 26 bytes, bitmaps useBITMAPINFOHEADER), whichtests/headers.cchecks.include/os2v2/: the 32-bit API (os2.h): the Dos calls (files and extended attributes, large files, memory, threads, semaphores and timers, queues, named pipes, sessions, exception handlers, modules and resources, national language support, messages, file systems and device IOCTLs), the error codes, and the Presentation Manager: windows and messages, frames, menus, dialogs, accelerators, the standard controls (buttons, entry fields, list boxes, combo boxes, scroll bars, MLEs, spin buttons), the clipboard, pointers and cursors (pmwin.h); presentation spaces, colors, lines, areas, paths, fonts and bitmaps (pmgpi.h); the window list and profiles (pmshl.h); and the file dialog (pmstddlg.h).APIENTRYis the C convention, which is what OS/2's_Systemis. The video, keyboard and mouse calls (Vio, Kbd, Mou), which are 16-bit on every OS/2, are inbsesub.h(see below).lib/call16.c: the thunk from 32-bit code into the 16-bit API, built intolibos2.awith a 16-bit stub for each call ofbsesub.h.lib/os2-16.lnk,lib/os2-32.lnk:importdirectives for every entry point of the OS/2 system DLLs (DOSCALLS, VIOCALLS, KBDCALLS, MOUCALLS, MSG, NLS, PMWIN, PMGPI and the rest). The driver gives them to the linker, which keeps only the imports a program uses. They were made bytools/gen-imports.pyfrom the DLL export lists in Open Watcom'sbld/os2api/lib286andlib386(the names and ordinals only).tools/:gen-imports.py(above),check-headers.py, which checks that every function the headers declare has an import, andcheck-syntax.sh, which compilestests/headers.cagainst both sets of headers and checks the sizes of the structures. CI runs both.os2rc.pyis a resource compiler (see below).gen-bsesub.pywritesbsesub.hand the 16-bit stubs.os2-imports.pylists the exports of a 16 or 32-bit DLL asimportdirectives, for linking the programs that use it.build-sysroot.sh: builds picolibc with its OS/2 layer, the compiler-rt builtins, and installs the headers and import lists:build-sysroot.sh 32 i386-os2makes the 32-bit sysroot, andbuild-sysroot.sh 16 i8086-msdosadds the 16-bit libraries to the DOS sysroot, inlib/os2/<model>(andlib/os2/<model>/dll, below).build-libcxx.sh: builds libc++ and libc++abi for 32-bit OS/2, with exceptions, RTTI and threads, into thei386-os2sysroot, and the unwinderruntime/unwind-sjlj.caslibunwind.a.build-libstdcxx.sh: adds GCC's libstdc++ to thei386-os2sysroot, next to libc++ (see below).examples/:hello.c,sysinfo.cand the PM windowpmhello.c(all 16 and 32-bit), and the 32-bit PM programpmnotes.c(a menu, accelerators, dialogs and strings, frompmnotes.rc).
How programs are built
32-bit. clang compiles to ELF objects; JWlink links them with
format os2 lx. crt0.o is first: OS/2 calls _start with the
environment and the command line, which become environ and argv. The
heap is address space reserved with DosAllocMem and committed as
malloc grows it. Programs are marked to run in a window (pmcompatible);
-mwindows marks a PM program. A DLL (-shared) starts with dllcrt0.o,
is initialized for each process that loads it, and has its data per
process. Mark its exports with __export (or VisualAge's _Export, or
__attribute__((dllexport))): clang leaves an -export: directive for
each in the object, which JWlink reads.
16-bit. clang compiles to OMF objects for a memory model (small,
medium, compact or large; OS/2 has no tiny or huge model) and JWlink links
them with format os2. The code is compiled as for 16-bit Windows: the
stack segment may not be DGROUP, so va_list and returned structures are
far. The heap is the rest of DGROUP, grown to 64 KiB with DosReallocSeg,
in the small and medium models, and a 64 KiB segment from DosAllocSeg in
the compact and large ones.
16-bit DLLs. -shared links a DLL with libentry.obj first. Like a
32-bit DLL it is initialized for each process that loads it
(initinstance) and each process gets its own DGROUP (manyautodata),
so the C library in the DLL keeps a state for each; the initialization
sets up the heap and environ and runs the constructors. OS/2 1.x has no
call when a DLL is freed, so destructors and atexit functions of a DLL
do not run. Export functions with __export __far __pascal; exported
functions load the DLL's DGROUP into DS themselves, as with __loadds.
A DLL runs on the stack of the thread that calls it, so SS is not DS
and a near pointer cannot point to a local. In the small and medium models
clang (-mwindows-dll, which -shared gives) keeps the locals whose
address escapes in a frame in DGROUP instead, from a 4 KiB region for each
thread taken from the DLL's heap; a DLL that needs more defines
unsigned short __os2_dll_frame_size = 8192;. The C library built this
way is in lib/os2/<model>/dll, which clang links with -shared. Compact
and large DLLs need none of it.
Vio, Kbd and Mou from 32-bit code. These calls are 16-bit far
Pascal functions in VIOCALLS, KBDCALLS and MOUCALLS. Each function of
bsesub.h is an inline one that lays out the 16-bit arguments and calls
__os2_call16 (lib/call16.c, in libos2.a, which the driver links).
That switches to a 16-bit stack of the thread's own and jumps to the
call's stub, 16-bit code in the program that calls the function (call far VIO16WRTTTY, fixed up by the loader as in a 16-bit program) and comes
back. Pointers become 16:16 ones through the tiles of OS/2's memory, and a
buffer that crosses a 64 KiB boundary is copied for the call.
Resources. tools/os2rc.py compiles a .rc file in the OS/2
toolkits' resource language (STRINGTABLE, MENU, ACCELTABLE, DLGTEMPLATE
with DIALOG, FRAME, WINDOW, CONTROL and the predefined controls, RCDATA,
ICON, POINTER, BITMAP, FONT and user types, with #include and #define
from the headers) and binds the resources into a program: a 32-bit one
gets one more object in its LX file, and a 16-bit one a segment for each
resource in its NE file, which DosGetResource2 returns:
clang --target=i386-pc-os2 -mwindows -Os pmnotes.c -o pmnotes.exe
python3 tools/os2rc.py -I include/os2v2 examples/pmnotes.rc pmnotes.exe
python3 tools/os2rc.py -l pmnotes.exe # list the resources
python3 tools/os2rc.py app.rc app16.exe # a 16-bit program: os21x headers
-r compiles to a .res file only, which a later run binds (-16 -r
for a 16-bit program, whose .rc sees the os21x headers).
The C library is picolibc with an OS/2 layer (libos/os2): a file
descriptor is the OS/2 file handle, and \n is written as \r\n to the
console and other devices. Files are binary. Directories are read with
opendir and readdir (DosFindFirst); getcwd, chdir, getpid and
getppid work too. chdir sets the current directory of a drive but does
not change the current drive.
OS/2 has no fork, so programs are started with DosExecPgm: system
runs COMSPEC (or CMD.EXE from the PATH) with /c, and
<process.h> has the spawn calls of OS/2's C compilers (spawnl,
spawnv, spawnvp, spawnle and the rest). P_WAIT returns the
program's exit code, and P_NOWAIT its process id, which waitpid and
wait wait for. exec starts the program and exits with its exit code.
A name without an extension gets .EXE, and the p forms search the
current directory and then the PATH. pipe, dup and dup2 work on
OS/2's handles, and popen runs the command with this program's end of
the pipe kept from it, so pclose gives it the end of its input.
Threads and C++
32-bit programs have threads: <threads.h> (C11 thrd_create, mutexes,
condition variables, tss) runs on DosCreateThread and OS/2's mutex and
event semaphores, the C library locks itself, and __thread variables,
errno among them, are per thread through clang's emulated TLS. The OS/2
Dos calls for threads and semaphores are in os2.h too.
C++ programs (clang++ --target=i386-pc-os2) link libc++ once
build-libcxx.sh has run:
BIN=/path/to/clang-retro/bin LLVM_SRC=/path/to/llvm \
sh build-libcxx.sh /path/to/clang-retro/i386-os2
Exceptions unwind through setjmp/longjmp-style function contexts (an LX
program keeps no tables for a table-driven unwinder), one chain per
thread. std::thread, std::mutex, std::condition_variable,
thread_local and steady_clock work. The terminate message does not
demangle names, which keeps programs smaller.
GCC's libstdc++ (15.2.0, compiled by clang) is the other C++ library.
build-libstdcxx.sh adds it, and -stdlib=libstdc++ selects it.
docs/libstdcxx.md tells how it is built and what it
has.
Testing
tests/ boots OS/2 Warp 3 from its two boot diskettes in QEMU and runs
programs from a diskette in drive B: (see tests/README.md). The 16-bit
examples pass in all four memory models, and the 32-bit ones pass too.
tests/dosapi.c exercises the 32-bit Dos calls (exception handlers,
semaphores, queues, named pipes, timers, NLS, file systems, messages and
extended attributes), tests/restest.c reads back the resources
os2rc.py bound into it with DosGetResource (16 and 32-bit), tests/posix.c tries
the directory calls of the C library, tests/spawn.c starts itself with
spawn, exec, system and popen and tries pipe (16 and 32-bit),
tests/threads.c the C11 threads, locks and per-thread errno,
tests/cxx.cpp C++ exceptions (in four threads at once too), RTTI,
iostreams and std::thread, and tests/vio.c the Vio, Kbd and
Mou calls from 32-bit code. tests/testdll.c is a 16-bit DLL that uses
the C library and tests/dllapp.c a program that calls it, and starts
itself again to check that the DLL's data is per process. All pass on
Warp 3, the DLL in all four memory models. Warp 3's boot
diskettes have no Presentation Manager, so PM programs are only built.
Not yet
- Threads calling into a 16-bit DLL at once: the 16-bit C library is built for one thread.
- The rarer Vio, Kbd and Mou calls from 32-bit code:
bsesub.hhas 39 common ones, and a call is added with a line intools/gen-bsesub.py. - Threads and C++ in 16-bit programs.
License
Copyright (C) clang-retro contributors.
The clang-retro OS/2 SDK is free software under the GNU Affero General Public
License, version 3 only (AGPL-3.0-only); the full text is in
LICENSE. If you distribute a program built with it, or let
people use a modified version over a network, you must offer the
program's complete source under the same license.
Each contributor keeps the copyright in their own work and contributes it under the same license; see CONTRIBUTING.md.