Path: utzoo!mnetor!uunet!lll-winken!lll-tis!ames!amdahl!nsc!taux01!orr From: orr@taux01.UUCP (Orr Michael ) Newsgroups: comp.lang.pascal Subject: Re: Global variables Message-ID: <539@taux01.UUCP> Date: 30 Mar 88 16:02:49 GMT References: <12654@brl-adm.ARPA> <529@taux01.UUCP> <4770@aw.sei.cmu.edu> Reply-To: orr@taux01.UUCP (Orr Michael ) Organization: National Semiconductor (Israel) Ltd. Lines: 46 Keywords: gloabl optimizing compilers aliasing In article <4770@aw.sei.cmu.edu> firth@bd.sei.cmu.edu.UUCP (Robert Firth) writes: >In article <529@taux01.UUCP> orr@taux01.UUCP (Orr Michael ) writes: >>As one of a team of compiler-writers, including a pascal compiler, >>let me note that global variables reduce the compiler's room for >>optimization. this is because any time you erite through a pointer, >>potentially you may be changing the value of any global var... > >I don't think that's right. For example, in Pascal, an explicit >pointer (^Thing) cannot point anywhere except into the heap, ie >at an object created by NEW. It cannot alias any global variable. > >That leaves only VAR parameters as potential aliases. Even here, >a VAR parameter of one type (VAR p: ThingA) cannot alias a global >variable of any other type (VAR g: ThingB). > >The extent to which this matters depends on people's coding style, >but with good Pascal programs (lots of different types) the amount >of possible aliasing is less than 5% (ie for any pointer you know >it cannot alias 95% or more of globals in scope). You are right, but not entirely right. While it's true that in STANDARD pascal a pointer can only point at objects on the heap, (i.e. objects you get by NEW), in the CTP compilers, there exists an extension allowing you to use 'addr_of(object)'. Moreover, some programmers (and let's not name names ;-) sometimes use a variant-record to explicitly manipulate the value of a pointer, making it possible to get the pointer to point ANYWHERE at all. since this practice is hard to guard against, usually you must be pessimistic. I would like to make two additional points : 1. I was talking about global variables in general, not only in PASCAL. (in the CTP compiler suite, by the time the optimizer sees the code, it has no way of telling that it was originally written in PASCAL, as the front-end has transformed it into an intermediate representation. Ditto for the code-generator) 2. There are other good reasons to avoid global variables, all well known. (mostly SE reasons). I was only trying to point out a relatively unknown disadvantage of global vars. I would guess that the reasons for the origianl objections cited, is not the one I raised, at least not mainly. -- orr@nsc.NSC.COM IBM's motto: Machines should work, {amdhal,decwrl,hplabs,pyramid,sun}!nsc!orr People should think. Orr's remark: Neither do. Disclaimer: Opinions, come home. All is forgiven. Papa.