⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 m+c+p

📁 <B>Digital的Unix操作系统VAX 4.2源码</B>
💻
字号:
.\"#@(#)m+c+p	4.1	Ultrix	7/17/90.if n .ds lq "".if n .ds rq "".if t .ds lq ``.if t .ds rq ''.po 1i.nr PO 1i.DA "May 15, 1984".TLUsing Modula-2 with Unix C and Berkeley Pascal.AUMichael L. Powell\*(DY.NH 1MODULA-2 CALLING SEQUENCE.PPThe Modula-2 calling sequence is.I compatible with the Unix C and Berkeley Pascal (hereafter called Pascal) calling sequences.Although it is not.I identicalto C and Pascal(in fact, C and Pascal are not the same!),it is possible to call procedures, pass parameters, and share variables..PPThe major difference between the calling sequences relates to the passingof multiword value parameters.Both Modula-2 and Pascal VAR parameters are passed by reference;since C doesn't have VAR parameters, the program must specify the address ofthe variable,which works out to be the same thing.All three languages pass single-word value parameters by pushing the values ontothe stack.Double-precision floating point value parameters are passed by pushingtwo words onto the stack.(In Pascal, reals are double-precision; in C, both floats and doubles arepassed as doubles;Modula-2 reals are not passed as double-precision automatically; therefore,for communication between Modula2 and Pascal or C, use longreals)..PPIn Pascal, multiword value parameters (arrays, records, sets, etc.) areall pushed onto the stack.In C, although struct parameters are passed by pushing the value onto the stack,array parameters are passed by pushing the address onto the stack.In Modula-2, multiword parameters (except longreals) are passed by reference(the called routine makes a copy of the parameter if necessary).Pascal routines called by Modula-2 should thus declare multiword parametersto be VAR parameters.In the (unlikely) case that the Pascal routine wishes to change a multiwordparameter that Modula-2 thinks is a value parameter,the Pascal routine must make a copy of the parameter in a local variable.C routines called by Modula-2 should always expect pointers, as they usuallydo anyhow.A Modula-2 routine called by Pascal should be declared in Pascalto accept VAR parameters for multiword values..PPA Modula-2 open array parameter is passed as two parameters:a pointer to the start of the array followed by the number of elements inthe array.Two special cases of interest are open arrays of byte and word.In these cases, the number of elements is the size of the variable,which may be of any type,in bytes and words (rounded up), respectively.For example,it would be possible to define the Unix read system call to have two parameters:the first, a file number, and the second, an open array of bytes for the buffer..PPTo access existing procedures that expect pointers to arrays but no counts,the @nocount attribute may be placed in the open array declaration.This causes the parameter to be just a pointer to the array. E.g.,.DS    procedure open(name : array @nocount of char; mode : cardinal);.DEpasses two parameters to the C open routine..NH 1DATA REPRESENTATION.PPRather than adopt the confusing and inconsistent allocation strategiesused by C and Pascal,Modula-2 currently has a simple-minded way of allocating data.Storage is allocated in bytes (for characters and booleans) or32-bit words (for everything else).Scalars are aligned on a multiple of their size.Records and arrays are aligned according to the alignment requirements oftheir fields and elements..PPThe size of the storage that variables or fields of a particular data type occupy may be specified by the @size attribute. E.g.,.DStype    Short = @size 16 integer;.DEdefines a 16-bit integer type.The default alignment is the size rounded up to a power of two bits (but nevermore than a 32 bits).The alignment, too, may be overridden by specifying the @align attribute, e.g.,.DStype    RegNum = @align 1 @size 3 [0..7];.DEallows a RegNum to start on any bit boundary..NH 1POINTERS.PPThe Modula-2 runtime allocates storage from a heap using its ownallocation and deallocation routines.Generally, these are different from (and much better than!) the Pascalnew and dispose routines or the C malloc and free routines.However, it is possible to generate and use pointers that are compatiblewith other languages..PPBy default, storage allocated from the Modula-2 heap has a check word storedat the beginning.This makes the validity check for pointers stronger, since storage that hasbeen freed will fail the test.Compiling a module without checking will prevent the word from being checked,but it will still be allocated and initialized,since other modules may still perform checking.This checking (and the extra word of storage) can be eliminated by declaringa pointer as follows:.DSUncheckedPtr = pointer @nocheck to Rec;.DE.LPNote that this form of declaration should be used for pointers generated bythe adr function..PPPointers may be allocated and deallocated using the Pascal (NEW/DISPOSE)or C (malloc/mfree)storage allocation routinesby declaring them as follows:.DSPascalPtr = pointer @pascal to Rec;CPtr = pointer @c to Rec;.DE.LP@pascal pointers have the Berkeley Pascal validity check applied to them;@c pointers are not checked..NH 1NAMES.PPThe external names of Modula-2 routines and statically allocated variables aregenerated by qualifying the local name with the names of the enclosing modulesand procedures.A procedure called NextToken in a module parse will be called _parse_NextToken.The leading _ is appended to names by both the C and Pascal compilers,so C or Pascal would call this routine parse_NextToken.(The Berkeley Pascal compiler can easily be modified to accept _ in the middleof variable names by adding "|| c == '_'" to the identifier while loop inyylex.c.).PPEach level of nesting inside a procedure or module adds qualifiers.E.g., the procedure SkipBlanks nested inside NextToken is called_parse_NextToken_SkipBlanks.However, because of differences in the displays, it is unlikely that you cansuccessfully call nested procedures between languages..PPAlthough this scheme works well for programs that use modules, current Pascaland C programs expect to have global, unqualified names.To make a procedure or variable have a global name (from the point of viewof C or Pascal programs),add the @external attribute before the name in the procedure or variabledeclaration.E.g.,.DSvar    @external errno : integer;procedure @external perror(msg : CString);.DE.PPIt is possible to create modules with Modula-2 definitionsthat are implemented in some other language.The unix library module is an example.Notice that errno is specified @external, because its name is unqualified,whereas argc is not, since runtime.c refers to it as unix_argc.When the implementation of a module is in some other language, it is stilla good idea to have a dummy implementation module for Modula-2.This provides an object module to contain the intermodule checking informationfrom the definition file..PPEach module must have an initialization routine.The initialization routine for module X is called X__init(X, two underscores, init), and has no parameters.These routines are called when the program starts up, and may be called morethan once (if several modules refer the module).The Modula-2 compiler generates code for its initialization routines to executethe body only once, even though it is called more than once.If you implement an initialization routine in C or Pascal, youwill need to do the same thing..PPThe initialization routine for a program module(one that is neither a definition module noran implementation module)is the main program.It is always called main, no matter what the module is called,so Unix can execute it..NH 1CAVEAT PROGRAMMER.PPCommunicating between languages can be difficult.Although we have taken pains to try to make this implementation of Modula-2compatible with C and Pascal, no guarantees are expressed or implied.If you must combine software in different languages, do it as cleanly aspossible and it will probably work.Problems in this area will not normally be considered bugs in the Modula-2compiler.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -