[comp.sys.amiga.tech] gadgets in 2.0

pepers@enme3.ucalgary.ca (Brad Pepers) (05/07/90)

I read the other day about how there is a new gadget library that does all
sorts of wonderful things for you. This is of course GREAT.... BUT.....

The calling convention of the functions was something like:

     funcion(param1,param2,...,paramN,tag1,val1,tag2,val2,....);

Where param? are necessary params and the rest are tag/value combinations
with no particular type or number of them.

My problem is this: while this is ok for C (but you don't get type checking
and "safe" things like that) its not ok for most other languages. There is no
way to declare a routine like that for Pascal, Modula2, etc... They just
don't allow variable parameter lengths. So these wonderful new functions
can't be used by anything but C and assem!!!

Why couldn't the gadget routines take a pointer to a variable length 2D
array with tag,value pairs? This would be supportable in all languages.

Am I right about this or is there something I've missed??

    Brad Pepers

navas@cory.Berkeley.EDU (David C. Navas) (05/07/90)

In article <1990May6.183020.17956@calgary.uucp> pepers@enme3.UUCP (Brad Pepers) writes:
[Speaking about varargs function calls]
>There is no
>way to declare a routine like that for Pascal, Modula2, etc... They just
>don't allow variable parameter lengths. So these wonderful new functions
>can't be used by anything but C and assem!!!

Wrongo...  Well actually, righto, but there are workarounds.. :)

>Why couldn't the gadget routines take a pointer to a variable length 2D
>array with tag,value pairs? This would be supportable in all languages.

They are, err, aren't err, okay  here's the story as I know it :)
There are two functional front-ends.  One is with varargs, the other
with array stuff.  Shmart, eh?
 
>Am I right about this or is there something I've missed??

Yes, Yes... :) :)
 
>    Brad Pepers

David Navas                                   navas@cory.berkeley.edu
"Excuse my ignorance, but I've been run over by my train of thought."  -me

dave@cs.arizona.edu (Dave Schaumann) (05/07/90)

In article <1990May6.183020.17956@calgary.uucp>, pepers@enme3.ucalgary.ca (Brad Pepers) writes:
<
<The calling convention of [gadget] functions was something like:
<
<     funcion(param1,param2,...,paramN,tag1,val1,tag2,val2,....);
<
<Where param? are necessary params and the rest are tag/value combinations
<with no particular type or number of them.
<
<My problem is this: while this is ok for C (but you don't get type checking
<and "safe" things like that) its not ok for most other languages.
>
<[...]
<
<Why couldn't the gadget routines take a pointer to a variable length 2D
<array with tag,value pairs? This would be supportable in all languages.
<
Why not write a 'glue' routine in asm to do just that?  It would take a list
as you suggested and create the appropriate stack frame as required by the
gadget routine being called.  You could even (assuming you have function/
procedure parameters in your language) create a single generic function
that would take a function name and a parameter list, create the stack frame,
and call the passed function.  Of course, you would probably want to add some
error checking capabilities...
<
<    Brad Pepers

Dave Schaumann		| "Strange women lying in ponds distributing swords
dave@cs.arizona.edu	|  is no basis for a system of government!"
FidoNet: 1/300/4	| 		-M. Python

jjszucs@cbmvax.commodore.com (John J. Szucs) (05/07/90)

In article <1990May6.183020.17956@calgary.uucp> pepers@enme3.UUCP (Brad Pepers) writes:
>
>I read the other day about how there is a new gadget library that does all
>sorts of wonderful things for you. This is of course GREAT.... BUT.....
>
>The calling convention of the functions was something like:
>
>     funcion(param1,param2,...,paramN,tag1,val1,tag2,val2,....);
>
>Where param? are necessary params and the rest are tag/value combinations
>with no particular type or number of them.
>
>My problem is this: while this is ok for C (but you don't get type checking
>and "safe" things like that) its not ok for most other languages. There is no
>way to declare a routine like that for Pascal, Modula2, etc... They just
>don't allow variable parameter lengths. So these wonderful new functions
>can't be used by anything but C and assem!!!
>
>Why couldn't the gadget routines take a pointer to a variable length 2D
>array with tag,value pairs? This would be supportable in all languages.
>
>Am I right about this or is there something I've missed??
>

The variable argument functions in 2.0 are called as follows:

void MyFunction()
{
	struct NewMenu MyNewMenu;
	struct Menu *MyMenu;

	/* initialize my new menu */
	...

	/* create MyMenu from specification in MyNewMenu with front pen of 0 */
	MyMenu=CreateMenusA(&MyNewMenu,GTMN_FrontPen,0,TAG_DONE,NULL);

	/* do other stuff */
}

If your language (or compiler) does not support variable argument functions,
you can pass an array of TagItems to a function that expects them, like this:

void MyFunction()
{
	struct NewMenu MyNewMenu;
	static struct TagItem MyTagList[] ={
		GTMN_FrontPen, 0,
		TAG_DONE, NULL,
	};

	/* Initialize my new menu */
	...

	/* create MyMenu from specification in MyNewMenu
	   and tags in MyTagList */
	MyMenu=CreateMenusA(&MyNewMenu,&MyTagList);

	/* do other stuff */
}

Basically, you can pass a pointer to an array of TagItems, with the type
of last one being TAG_DONE, and the results will be the same as passing
a set of tags as parameters to the function.

Notes:

CreateMenusA is a function that accepts a pointer to a NewMenu structure
and a list of tags and returns a pointer to a Menu structure.

TAG_DONE is a special tag type indicating the end of the list of tags.

>    Brad Pepers

================================================================================
|| John J. Szucs                    || The opinions expressed are my own and  ||
|| Systems Evaluation Group         || in no way represent the opinions or    ||
|| Product Assurance Department     || policies of Commodore Technology, Inc. ||
|| Commodore Technology, Inc.       || or any associated entity.              ||
================================================================================
...{rutgers|uunet|pyramid}!cbmvax!jjszucs
jjszucs@cbmvax.commodore.com

martint@altitude.CAM.ORG (Martin Taillefer) (05/07/90)

In article <24783@pasteur.Berkeley.EDU> navas@cory.Berkeley.EDU.UUCP (David C. Navas) writes:
>In article <1990May6.183020.17956@calgary.uucp> pepers@enme3.UUCP (Brad Pepers) writes:
>[Speaking about varargs function calls]
>>There is no
>>way to declare a routine like that for Pascal, Modula2, etc... They just
>>don't allow variable parameter lengths. So these wonderful new functions
>>can't be used by anything but C and assem!!!
>
>Wrongo...  Well actually, righto, but there are workarounds.. :)

Yes, the new gadtools library accepts tag arguments, as do many of the new
system routines. The varargs magic though is in the C front-end and
has in reality nothing to do with the way the system actually works.

What I mean is that the system actually DOES expect only a POINTER to an
array of TAG items. The C compiler converts the varargs parameters into such an
array and then passes the address of the array to the ROM code.

Tags are very flexible and extensible, which is why they are being used. 
It is clear however that this scheme was not meant for languages with decent
type checking. And since I know the guy(s) that thought up tags does not like
type-checked languages, it's no surprise.

From a language such as Modula-2, it is annoying to do tags. Using the
current compilers, you'd need to declare an array of tags and fill them in
before calling the routine. This is very inconvenient. Using array
constructors as described in the forthcming ISO M2 standrad allows these
arrays to be declared inline, in the function call, which effectively
gives you varargs.

The only remaining problem is with type checking. The main reason I chose
M2 was for type checking, the fact that the compiler will catch my mistakes
for me, instead of having the GURU find them. There's also a tremendous
documentation value in fully typed system structures. I don't really
see a solution to this problem, it IS very annoying.

In any case, I think it will be very annoying to do tags with the current
M2 compilers. We're working on something to address this I hope :-)

-- 
----------------------------------------
Martin Taillefer   INTERNET: martin@pnt.CAM.ORG
UUCP: uunet!philmtl!altitude!martint   TEL: 514/640-5734   BIX: vertex

barrett@jhunix.HCF.JHU.EDU (Dan Barrett) (05/07/90)

In article <1990May6.183020.17956@calgary.uucp> pepers@enme3.UUCP (Brad Pepers) writes:
>There is no way to declare a routine like that for Pascal, Modula2, etc...
>They just don't allow variable parameter lengths.

	You don't have to *declare* these routines... just call them.
And Pascal and Modula2 can indeed call routines that have an arbitrary
number of arguments.  Consider "writeln()", for example.

	I don't see any problem here.

                                                        Dan

 //////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
| Dan Barrett, Department of Computer Science      Johns Hopkins University |
| INTERNET:   barrett@cs.jhu.edu           |                                |
| COMPUSERVE: >internet:barrett@cs.jhu.edu | UUCP:   barrett@jhunix.UUCP    |
 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/////////////////////////////////////

martint@altitude.CAM.ORG (Martin Taillefer) (05/07/90)

In article <5185@jhunix.HCF.JHU.EDU> barrett@jhunix.UUCP (Dan Barrett) writes:
>	You don't have to *declare* these routines... just call them.
>And Pascal and Modula2 can indeed call routines that have an arbitrary
>number of arguments.  Consider "writeln()", for example.

First off, WriteLn is a special case in Pascal. There is no general-purpose
variable argument mechanism. Second off, there is no WriteLn (in the Pascal 
sense) in Modula-2.

-- 
----------------------------------------
Martin Taillefer   INTERNET: martin@pnt.CAM.ORG
UUCP: uunet!philmtl!altitude!martint   TEL: 514/640-5734   BIX: vertex

jimm@amiga.UUCP (Jim Mackraz) (05/08/90)

(Martin Taillefer) writes:

)Tags are very flexible and extensible, which is why they are being used. 
)It is clear however that this scheme was not meant for languages with decent
)type checking. And since I know the guy(s) that thought up tags does not like
)type-checked languages, it's no surprise.

Hey, I didn't invent them, I stole them.  Anyway, it's nice to see
you on usenet now, Martin, keeping me honest  ... ;^)

)From a language such as Modula-2, it is annoying to do tags. Using the
)current compilers, you'd need to declare an array of tags and fill them in
)before calling the routine. This is very inconvenient. Using array
)constructors as described in the forthcming ISO M2 standrad allows these
)arrays to be declared inline, in the function call, which effectively
)gives you varargs.

The alternative is to use structures; declare them and fill them
in, plus specify in a version, size, or flags field just which of
the continually evolving parameters you are specifying.  But for
the type checking (and a little space), doing tags via arrays
isn't much worse.

)The only remaining problem is with type checking. The main reason I chose
)M2 was for type checking, the fact that the compiler will catch my mistakes
)for me, instead of having the GURU find them. There's also a tremendous
)documentation value in fully typed system structures. I don't really
)see a solution to this problem, it IS very annoying.

Yeah, I think that's the breaks with the languages we use these days.
At least now, if you get a Guru, you know it's in your tags code ;^)
Some day Mr. Language will be able to associate a tagged data item
with its type.

Really nice to see you out here.

	jimm

-- 
--------------------------------------------------	- opinions by me
"This voice console is a *must*.  I press Execute. 
 `Hello, I know that you've been feeling tired.
  I bring you love and deeper understanding.' "		-lyrics by Kate Bush

martint@altitude.CAM.ORG (Martin Taillefer) (05/08/90)

In article <5692@amiga.UUCP> jimm@superman.UUCP (Jim Mackraz) writes:
>Hey, I didn't invent them, I stole them.  Anyway, it's nice to see
>you on usenet now, Martin, keeping me honest  ... ;^)

Well, at least you recognized yourself! :-) Glad to be here. I've been
watching for the last few month and have decided to firmly join in the
discussions. Hey, somebody's goot stand up for M2 around here!

>The alternative is to use structures; declare them and fill them
>in, plus specify in a version, size, or flags field just which of
>the continually evolving parameters you are specifying.  But for
>the type checking (and a little space), doing tags via arrays
>isn't much worse.

Actually, when I said arrays, I really meant arrays of Tag records.
With ISO M2, you can declare these in-line no problem. I haven't figured out
the exact syntax I'm gona use to declare the tags and associated routines
yet. I'm waiting for you guys to finish up the includes and autodocs before I
start converting all that stuff to M2.

>Yeah, I think that's the breaks with the languages we use these days.
>At least now, if you get a Guru, you know it's in your tags code ;^)
>Some day Mr. Language will be able to associate a tagged data item
>with its type.

I've been trying to come up with a scheme to allow me to associate
a tag value with a given type. The only method I currently see is to
declare a different tag structure for every possible type. Nah, even that
wouldn't work.

What would be required would some sort of dynamic typing, where the type
info is carried along with the actual data. This would also require extensive
run-time checks. Yuk. This is useful in experimental languages but
won't do us much good in real-life systems applications.

>	jimm


-- 
----------------------------------------
Martin Taillefer   INTERNET: martin@pnt.CAM.ORG
UUCP: uunet!philmtl!altitude!martint   TEL: 514/640-5734   BIX: vertex

andrew@teslab.lab.OZ (Andrew Phillips 289 8712) (05/09/90)

In article <5185@jhunix.HCF.JHU.EDU> barrett@jhunix.UUCP (Dan Barrett) writes:
>And Pascal and Modula2 can indeed call routines that have an arbitrary
>number of arguments.  Consider "writeln()", for example.

This is incorrect.  Writeln() is not a real function/procedure but is
part of the Pascal language.

There is also a problem for C in that although C allows a variable
number of parameters some compilers have a maximum limit (e.g. 20).

Andrew.
-- 
Andrew Phillips (andrew@teslab.lab.oz.au) Phone +61 (Aust) 2 (Sydney) 289 8712

peter@cbmvax.commodore.com (Peter Cherna) (05/09/90)

In article <609@teslab.lab.OZ> andrew@teslab.lab.oz.au (Andrew Phillips  289 8712) writes:
>In article <5185@jhunix.HCF.JHU.EDU> barrett@jhunix.UUCP (Dan Barrett) writes:
>>And Pascal and Modula2 can indeed call routines that have an arbitrary
>>number of arguments.  Consider "writeln()", for example.
>
>This is incorrect.  Writeln() is not a real function/procedure but is
>part of the Pascal language.

Pascal has nice strong typing rules.  It was designed to be a good choice
as a first language (not to denigrate its power at all, but rather to
point out its singular strength).  Sadly, when I was in school at
the level where people were learning to program (not that long ago, I'm
not that old :-) ), the mindset was that people should begin with BASIC
before moving on to the advanced languages like Pascal or C or Fortran.
The fortunate among us were able to forget the mistakes we learned in BASIC
and learn to organize and structure ourselves the way Pascal required.

Pascal requires that all functions have fixed strongly-typed arguments.
Excellent idea, but even they had to admit that the real-world demands
some tradeoffs of idealism/structure vs. efficiency, and writeln() was
Pascal's sheepish concession.  Conversely, C has gone too far, though
ANSI has brought it back some.

>There is also a problem for C in that although C allows a variable
>number of parameters some compilers have a maximum limit (e.g. 20).

Generally not on the Amiga.  The standard C-style calling convention
puts arguments on the stack, so you can pile them up until you blow
your stack.

>Andrew.
>-- 
>Andrew Phillips (andrew@teslab.lab.oz.au) Phone +61 (Aust) 2 (Sydney) 289 8712

     Peter
--
     Peter Cherna, Software Engineer, Commodore-Amiga, Inc.
     {uunet|rutgers}!cbmvax!peter    peter@cbmvax.cbm.commodore.com
My opinions do not necessarily represent the opinions of my employer.
"If you insist on spending $10000 on a 68030 technology, may we humbly
suggest you buy three Amiga 3000's."