gert@nikhefh.UUCP (05/20/87)
In response to the denial of a few persons that Megamax won't accept passing of structures to functions: The Megamax compiler will accept the C code. It will not issue any warning, but the code it generates it totally wrong! This is the test program: typedef struct { int t1; int t2; } test; main () { test test; ftest ( test ); } ftest ( t ) test t; { t.t1 = 1; t.t2 = 2; } Megamax disassembler V1.0, Copyright (C) 1986 by Megamax Inc. CODE SEG "main" main:/* global */ LINK A6,L$0 PEA -4(A6) !!! here main pushes the ADDRESS of the structure test, when it should push the contents of it. JSR ftest(PC) ADDQ.L #4,A7 L1: UNLK A6 RTS L$0: .EQU #-4 _initargcv:/* global */ RTS ftest:/* global */ LINK A6,L$2 MOVE #1,8(A6) !!! this would be t.t1 = 1, the ftest function apparantly expects the structure to be on stack, unfortunately only the address of the structure is on the stack. (Bomb!!) MOVE #2,10(A6) L3: UNLK A6 RTS L$2: .EQU #0 This example was compiled with version 1.1 of the Megamax compiler. I hope this makes clear that Megamax does not 'allow' structure passing. In one of my previous message I said that Megamax would also not allow declaration of variables somewhere in a function (not neccesarily at the top). In that case I was wrong, sorry. I still think I remember reading something to that extend in one of the manuals, but it may very well have been for another C compiler. Gert Poletiek.
john@viper.Lynx.MN.ORG (John Stanley) (05/22/87)
In article <280@nikhefh.UUCP> gert@nikhefh.UUCP (Gert Poletiek) writes: > >In response to the denial of a few persons that Megamax won't accept >passing of structures to functions: The Megamax compiler will accept the C >code. It will not issue any warning, but the code it generates it >totally wrong! Passing a structure to a function is not supported in the definition of how-C-works... Take a look in your handy dandy K&R (page 121). It tells you that passing structures to a function is -not-supported-. The compilers that have been created since then handle the problem in (unfortunately) one of three incompatable ways... 1) Since K&R said it was not supported, some compilers will flag a structure-parameter as an error... (nuf said, next...) 2) Since passing a structure isn't supported in K&R C, if you include a structure name in a parameter list, you must really mean to pass the address of that structure as a pointer. The compiler thus handles structure and array references in the exact same way. (A few of the compilers that do this (unfortunately, not megamax) go the whole route and also convert a structure receiver on the functions side into a structure pointer and converts structure references within the function into indirect references to match.) Most of the compilers in this class assume you have a struct pointer in the parameter list of the function. If you have a struct instead, the compiler assumes you plan to reference the arguments as a structure for some purpose and the references to the structure elements reference the individual parameters... 3) Some "C" compilers take your word that you do, in fact, want to stick a copy of the entire structure on the stack and pass it to the function. Megamax is aparently one of the class 2 structure passing compilers, but without the (slightly illegal) conversion on the function end of things. Summery: Yes, megamax isn't handling it correctly, but then, you can't count on any program that passes structures to work on any randomly selected C compiler... Apparently you learned to write C on a class 3 compiler. Anything you do that involves passing structures will-not-work on (at a guess) 75% of all compilers. It's a habit you would be better off un-learning as soon as possible. Solution? When you're learning a new compiler or trying to write portable code, make the assumption that passing a struct doesn't work. Pass a pointer to the structure or to a copy of the structure and have a much better chance of your code being portable (and running)... --- John Stanley (john@viper.UUCP) Software Consultant - DynaSoft Systems UUCP: ...{amdahl,ihnp4,rutgers}!{meccts,dayton}!viper!john
peter@megamax.UUCP (05/27/87)
> > In response to the denial of a few persons that Megamax won't accept > passing of structures to functions: The Megamax compiler will accept the C > code. It will not issue any warning, but the code it generates it > totally wrong! > > > Megamax disassembler V1.0, Copyright (C) 1986 by Megamax Inc. > > CODE SEG "main" > main:/* global */ > LINK A6,L$0 > PEA -4(A6) !!! here main pushes the ADDRESS of the structure > test, when it should push the contents of it. > JSR ftest(PC) > ADDQ.L #4,A7 > L1: > UNLK A6 > RTS > L$0: .EQU #-4 > _initargcv:/* global */ > RTS > ftest:/* global */ > LINK A6,L$2 > MOVE #1,8(A6) !!! this would be t.t1 = 1, the ftest function > apparantly expects the structure to be on > stack, unfortunately only the address of the > structure is on the stack. (Bomb!!) > MOVE #2,10(A6) > L3: > UNLK A6 > RTS > L$2: .EQU #0 > > > > This example was compiled with version 1.1 of the Megamax compiler. > I hope this makes clear that Megamax does not 'allow' structure passing. > > Gert Poletiek. Structure passing and assignment have been a part of the Megamax C language since the very first day on the Atari. The code that was generated by the compiler using the example program that was sent is as follows: This is the test program: typedef struct { int t1; int t2; } test; extern test ftest(); main() { test test; ftest(test); } ftest(t) test t; { t.t1 = 1; t.t2 = 2; return t; } This is the disassembled object module: CODE SEG "main" main: /* global */ LINK A6,L$0 LEA -4(A6),A1 SUBA #4,A7 MOVE.L A7,A0 MOVEQ #1,D0 JSR _blockmv(PC) <-- place structure onto the stack JSR ftest(PC) <-- do structure thing ADDQ.L #4,A7 LEA -4(A6),A0 MOVEQ #1,D0 JSR _blockmv(PC) <-- assign struct with returned struct from function L1: UNLK A6 RTS L$0: .EQU #-4 BSS SEG "bss" 1struct: .WORD #4 <-- hold place for returned structure CODE SEG "main" ftest: /* global */ LINK A6,L$2 MOVE #1,8(A6) <-- access members of structure on the stack MOVE #2,10(A6) LEA 8(A6),A1 LEA 1struct(A4),A0 MOVEQ #1,D0 JSR _blockmv(PC) <-- hold place for returning structure LEA 1struct(A4),A1 UNLK A6 RTS L$2: .EQU #0 This source file was compiled with Megamax C v1.1. Even though it is generally not good practice to depend on structure passing with other C systems the Megamax system definitely supports it. If your Megamax C compiler doesn't seem to generate the proper code, send in your disks. The upgrade to version 1.1 of the Megamax Development System is free (please send Self Addressed and Stamped Disk Mailer.)