;[]-----------------------------------------------------------------[] ;| RULES.ASI -- Rules & Structures for assembler | ;[]-----------------------------------------------------------------[] ; ; C/C++ Run Time Library - Version 1.5 ; ; Copyright (c) 1987, 1994 by Borland International ; All Rights Reserved. ; ;[]------------------------------------------------------------[] ;| | ;| Calling Conventions | ;| | ;[]------------------------------------------------------------[] ; The calling convention definitions correspond to those in _DEFS.H. ; ; _RTLENTRY is the default calling convention for the RTL. It is ; stdcall on OS/2, and cdecl on Win32 and NT. ; ; _USERENTRY is the default calling convention for user functions. It is ; stdcall on OS/2, and cdecl on Win32 and NT. ; ; _RTLENTRYF is normally identical to _RTLENTRY, but can be changed ; to pascal by defining __PAS__. cdecl equ 1 pascal equ 2 fastcall equ 3 fortran equ 4 stdcall equ 5 syscall equ 6 ifdef _BUILDRTLDLL _EXPFUNC equ publicdll else _EXPFUNC equ public endif ifdef __WIN32__ _RTLENTRY equ cdecl _USERENTRY equ cdecl APIENTRY equ stdcall else _RTLENTRY equ stdcall _USERENTRY equ stdcall APIENTRY equ cdecl endif ifdef __PAS__ _RTLENTRYF equ pascal else _RTLENTRYF equ _RTLENTRY endif ;[]------------------------------------------------------------[] ;| | ;| Segment Declarations Macros | ;| | ;[]------------------------------------------------------------[] Code_Seg@ MACRO ;; Open a Code Segment _TEXT SEGMENT DWORD USE32 PUBLIC 'CODE' ASSUME CS:FLAT ENDM Code_EndS@ MACRO ;; Close a Code Segment _TEXT ENDS ENDM Data_Seg@ MACRO ;; Open a Data Segment (initialized) _DATA SEGMENT DWORD USE32 PUBLIC 'DATA' ASSUME DS:FLAT ENDM Data_EndS@ MACRO ;; Close a Data Segment (initialized) _DATA ENDS ENDM BSS_Seg@ MACRO ;; Open a Data Segment (un-initialized) _BSS SEGMENT DWORD USE32 PUBLIC 'BSS' ASSUME DS:FLAT ENDM BSS_EndS@ MACRO ;; Close a Data Segment (un-initialized) _BSS ENDS ENDM Const_Seg@ MACRO ;; Open a CONST Segment ifndef MASM NOWARN RES endif CONST SEGMENT DWORD USE32 PUBLIC 'CONST' ifndef MASM WARN RES endif ASSUME DS:FLAT ENDM Const_EndS@ MACRO ;; Close a CONST Segment ifndef MASM NOWARN RES endif CONST ENDS ifndef MASM WARN RES endif ENDM Init_Seg@ MACRO ;; Open a INIT Segment _INIT_ SEGMENT WORD USE32 PUBLIC 'INITDATA' ENDM Init_EndS@ MACRO ;; Close a INIT Segment _INIT_ ENDS ENDM Exit_Seg@ MACRO ;; Open a EXIT Segment _EXIT_ SEGMENT WORD USE32 PUBLIC 'EXITDATA' ENDM Exit_EndS@ MACRO ;; Close a EXIT Segment _EXIT_ ENDS ENDM Header@ MACRO .386P ifndef MASM model flat endif Code_Seg@ Code_EndS@ Data_Seg@ Data_EndS@ BSS_Seg@ BSS_EndS@ ASSUME CS:FLAT, DS:FLAT, SS:FLAT, ES:FLAT ENDM ;[]------------------------------------------------------------[] ;| | ;| C Naming Convention Macros | ;| | ;[]------------------------------------------------------------[] UNDERSCORE EQU 1 ExtSym@ MACRO Sym, sType, sName IFNB IF sName eq pascal NAMING = 0 ELSE NAMING = UNDERSCORE ENDIF ENDIF IF NAMING EXTRN _&Sym : sType Sym&@ equ _&Sym ELSE EXTRN Sym : sType Sym&@ equ Sym ENDIF ENDM PubSym@ MACRO Sym, Definition, sName IFNB IF sName eq pascal NAMING = 0 ELSE NAMING = UNDERSCORE ENDIF ENDIF IF NAMING PUBLIC _&Sym _&Sym Definition Sym&@ equ _&Sym ELSE PUBLIC Sym Sym Definition Sym&@ equ Sym ENDIF ENDM Static@ MACRO Sym, Definition, sName IFNB IF sName eq pascal NAMING = 0 ELSE NAMING = UNDERSCORE ENDIF ENDIF IF NAMING _&Sym Definition Sym&@ equ _&Sym ELSE Sym Definition Sym&@ equ Sym ENDIF ENDM PAGE ;[]------------------------------------------------------------[] ;| | ;| Macros that are Data Size Dependent | ;| | ;[]------------------------------------------------------------[] DPTR_ equ DWORD PTR dPtrSize equ 4 dPtr@ MACRO Sym, VALUE, sName ;; Static Data pointer Static@ Sym,
, sName ENDM dPtrPub@ MACRO Sym, VALUE, sName ;; Global Data Pointer PubSym@ Sym,
, sName ENDM dPtrExt@ MACRO Sym, sName ;; External Data Pointer ExtSym@ Sym, DWORD, sName ENDM ;[]------------------------------------------------------------[] ;| | ;| Macros that are Code Size Dependent | ;| | ;[]------------------------------------------------------------[] CPTR_ equ DWORD PTR cPtrSize equ 4 cPtr@ MACRO Sym, VALUE, sName ;; Static Function pointer Static@ Sym,
, sName ENDM cPtrPub@ MACRO Sym, VALUE, sName;; Global Function Pointer PubSym@ Sym,
, sName ENDM cPtrExt@ MACRO Sym, sName ;; External Function Pointer ExtSym@ Sym, DWORD, sName ENDM ;[]------------------------------------------------------------[] ;| | ;| Miscellaneous Definitions | ;| | ;[]------------------------------------------------------------[] ; C constants false equ 0 ; Beware ! For incoming parameters, non-false = true. true equ 1 ; For results, we generate the proper numbers. lesser equ -1 ; Incoming, lesser < 0 equal equ 0 greater equ 1 ; Incoming, greater > 0 ; Some convenient structures to access the parts of larger objects. _twoBytes STRUC BY0 db ? BY1 db ? _twoBytes ENDS _fourWords STRUC W0 dw ? W1 dw ? W2 dw ? W3 dw ? _fourWords ENDS _twoDwords STRUC DD0 dd ? DD1 dd ? _twoDwords ENDS _aFloat STRUC double dq ? _aFloat ENDS _eFloat STRUC longdouble dt ? _eFloat ENDS ;[]------------------------------------------------------------[] ;| | ;| Macros for function linkage, parameters, and locals | ;| | ;[]------------------------------------------------------------[] ; The following sets of macros allow you to declare function ; parameters and local variables, save register variables, and ; set up the stack frame. ; ; A typical function that used all of these macros would like like this: ; ; Func@ funcname, public, cdecl, , ; ; Locals@ , ; ; Link@ ebx, esi, edi ; ... function code ... ; Unlink@ ebx, esi, edi ; Return@ ; ; EndFunc@ funcname ; ; A call to such a function would like like: ; ; Call@ funcname ; ; Parameters and variables declared in this fashion can be referred to ; directly in the function code, but they are untyped. Therefore, explicit ; type conversion (e.g. "word ptr") may be required in some cases to avoid ; assembler warnings. ; ; The list of parameters to a function can be empty. ; ; The Locals@ macro is optional, but at must be present if Link@ is used. ;---------------------------------------------------------------------- ; Name Mangle@ - generate a mangled name with a prefix and suffix Mangle@ macro prefix, name, suffix, calltype, size if calltype eq stdcall ifdef __WIN32__ ;prefix _&name&@&size suffix prefix name suffix else prefix _&name suffix endif else if calltype eq pascal or calltype eq syscall prefix name suffix else if calltype eq cdecl prefix _&name suffix else %out Invalid calling convention &calltype for &name .err endif endif endif endm ;---------------------------------------------------------------------- ; Name OneParm@ - process the declaration of a function argument OneParm@ macro type, name local Psize@ ifidni , Psize@ = dword else ifidni , Psize@ = dword else ifidni , Psize@ = qword else ifidni , Psize@ = tbyte else ifidni , Psize@ = dword else ifidni , Psize@ = dword else ifidni , Psize@ = dword else Psize@ = type endif endif endif endif endif endif endif &name&_o = ParmOff@ name equ [ebp + &name&_o] ParmOff@ = ParmOff@ + Psize@ ParmOff@ = (ParmOff@ + 3) and 0fffch endm ;---------------------------------------------------------------------- ; Name Func@ - declare a C function ; ; Usage Func@ name, pubtype, calltype, [parm, ...] ; ; Notes Use this macro to declare a C function. ; pubtype: public, publicdll, static ; calltype: _RTLENTRY, _RTLENTRYF, cdecl, pascal, stdcall ; parm... : parameter list ; ; The name of the function is mangled according the to ; calling convention indicated by calltype. To refer to ; the name, use the symbol "name@" instead of the mangled name. ; The function is local to the current module, unless ; the second parameter to the macro is "public". ; ; Parm... are the actual parameters to the function ; itself. Each parameter is of the form , where ; type is int, long, char, double, longdouble, pointer, ; or simply the size in bytes of the parameter; and ; name is the name of the parameter. Func@ macro name, pubtype, calltype, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8 ParmOff@ = 8 LocalOff@ = 0 irp arg, ifnb OneParm@ arg endif endm ifidni , Mangle@ public, name, <>, calltype, %ParmOff@-8 endif ifidni ,<_EXPFUNC> Mangle@ &pubtype, name, <>, calltype, %ParmOff@-8 endif Mangle@ <>, name, , calltype, %ParmOff@-8 Mangle@ <&name&@ equ>, name, <>, calltype, %ParmOff@-8 if calltype eq cdecl or calltype eq syscall PopParms@ = 0 ; callee doesn't pop parms &name&_n@ equ ParmOff@-8 else PopParms@ = 1 ; callee pops parms endif CallType@ = calltype ; save calltype for EndFunc@ endm ;---------------------------------------------------------------------- ; Name EndFunc@ - mark the end of a C function ; ; Usage EndFunc@ name, calltype ; ; Notes Use this macro to mark the end of a C function that ; was declared with a Func@ macro. EndFunc@ macro name Mangle@ <>, name, , CallType@, %ParmOff@-8 endm ;---------------------------------------------------------------------- ; Name Call@ - call a C function, pop parameters if necessary ; ; Usage Call@ name ; ; Notes Use this macro to call a C function that was declared with ; a Func@ macro. The parameters to the functions are popped ; if the function uses the cdecl calling convention. ; ; NOTE: Do not use this macro to call a function that takes ; a variable number of arguments! Call@ macro name call name&@ ifdef &name&_n@ if &name&_n@ ne 0 add esp, &name&_n@ endif endif endm ;---------------------------------------------------------------------- ; Name Locals@ - declare local variables. ; ; Usage Locals@ , ... ; type: int, long, char, double, longdouble, pointer, ; or simply the size in bytes of the variable ; name: the name of the local variable ; ; Notes The Locals@ macro must appear before the Link@ macro ; in a function. OneLocal@ macro type, name local Lsize@ ifidni , Lsize@ = dword else ifidni , Lsize@ = qword else ifidni , Lsize@ = tbyte else ifidni , Lsize@ = dword else ifidni , Lsize@ = dword else ifidni , Lsize@ = dword else Lsize@ = type endif endif endif endif endif endif LocalOff@ = LocalOff@ - Lsize@ &name&_o = LocalOff@ name equ [ebp + &name&_o] endm Locals@ macro arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8 irp arg, ifnb OneLocal@ arg endif endm endm ;---------------------------------------------------------------------- ; Name Link@ - save register variables and set up stack frame ; ; Usage Link@ [regname ...] ; regname: ebx, esi, or edi ; ; Notes This macro creates a stack frame with space for ; local variables, and pushes the specified registers ; in the specified order. Use this macro only AFTER ; local variables are declared Locals@ macro. Link@ macro reg1,reg2,reg3 push ebp mov ebp, esp if LocalOff@ lea esp, [ebp + LocalOff@] endif ifnb push reg1 endif ifnb push reg2 endif ifnb push reg3 endif endm ;---------------------------------------------------------------------- ; Name Unlink@ - restore register variables and remove stack frame ; ; Usage Unlink@ [regname ...] ; regname: ebx, esi, or edi ; ; Notes This macro pops the specified registers in reverse ; order, then removes the stack frame set up by a previous ; Link@ macro. Unlink@ macro reg1,reg2,reg3 ifnb pop reg3 endif ifnb pop reg2 endif ifnb pop reg1 endif if LocalOff@ mov esp, ebp endif pop ebp endm ;---------------------------------------------------------------------- ; Name Return@ - return and pop parameters ; ; Usage Return@ ; ; Notes Return@ executes a ret instruction that pops the parameters ; to the function. Return@ macro if ParmOff@ ne 8 if PopParms@ ret ParmOff@ - 8 else ret endif else ret endif endm ;---------------------------------------------------------------------- ; Name ExtFunc@ - define an external function ; ; Usage ExtFunc@ name, calltype, parmsize ; ; Notes The ExtFunc@ macro defines an external function. Name ; specifies the name of the function. Calltype specifies ; the calling convention (_RTLENTRY, _RTLENTRYF, cdecl, pascal, ; or stdcall). Parmsize specifies the number of bytes of ; parameters passed to the function. ExtFunc@ macro name, calltype, parmsize Mangle@ extrn, name, <:near>, calltype, parmsize Mangle@ <&name&@ equ>, name, <>, calltype, parmsize if calltype eq cdecl or calltype eq syscall &name&_n@ equ parmsize endif endm ;---------------------------------------------------------------------- ; Name Init@ - define an _INIT_ record ; ; Usage Init@ name, prior, calltype ; ; Notes The Init@ macro defines an _INIT_ record that causes a ; function to be called at startup time. ; ; name The name of the external initialization function; ; the name will be appropriate mangled according to calltype. ; prior The priority of the function (0 == highest). ; calltype Must be _RTLENTRY, _RTLENTRYF, cdecl, pascal, or stdcall. Init@ macro name, prior, calltype .386P ifndef MASM model flat endif ASSUME CS: FLAT Init_Seg@ db 0 db prior Mangle@
, name, <>, calltype, 0 ifdef MSLINK db 10 dup (0) ; filler for NT's paragraph alignment endif Init_EndS@ endm