Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with global variables defined in the module code #2

Closed
sifislag opened this issue Jun 29, 2018 · 1 comment
Closed

Issues with global variables defined in the module code #2

sifislag opened this issue Jun 29, 2018 · 1 comment

Comments

@sifislag
Copy link

sifislag commented Jun 29, 2018

Hello,
I have some issues with variables defined in the module code(which are global). If they are constants they are never written as such with a AstGlobalWrite instruction but if they are used in a function of the same module, a constant var is used with the correct value.
If they are not constants they do not have a corresponding AstGlobalWrite instruction unless that variable is used on a function defined in the same module.
example:

#glovar.py
globVar = "Foo"
globVar2 = int(3)

Produces:

BB0
BB1
0   v5 = invokestatic < PythonLoader, Lstr, import()Lstr; > @0 exception:v6glovar.py [4:0] -> [5:17] [5=[str]]
1   v8 = invokestatic < PythonLoader, Lfloat, import()Lfloat; > @1 exception:v9glovar.py [4:0] -> [5:17] [8=[float]]
2   v11 = invokestatic < PythonLoader, Lint, import()Lint; > @2 exception:v12glovar.py [4:0] -> [5:17] [11=[int]]
3   v14 = invokestatic < PythonLoader, L__name__, import()L__name__; > @3 exception:v15glovar.py [4:0] -> [5:17] [14=[__name__]]
4   v17 = invokestatic < PythonLoader, LFalse, import()LFalse; > @4 exception:v18glovar.py [4:0] -> [5:17] [17=[False]]
5   v20 = invokestatic < PythonLoader, LTrue, import()LTrue; > @5 exception:v21glovar.py [4:0] -> [5:17] [20=[True]]
6   v23 = invokestatic < PythonLoader, LNone, import()LNone; > @6 exception:v24glovar.py [4:0] -> [5:17] [23=[None]]
7   v26 = invokestatic < PythonLoader, Lprint, import()Lprint; > @7 exception:v27glovar.py [4:0] -> [5:17] [26=[print]]
8   v29 = invokestatic < PythonLoader, Lsuper, import()Lsuper; > @8 exception:v30glovar.py [4:0] -> [5:17] [29=[super]]
9   v32 = invokestatic < PythonLoader, Llen, import()Llen; > @9 exception:v33glovar.py [4:0] -> [5:17] [32=[len]]
10   v35 = invokestatic < PythonLoader, Lopen, import()Lopen; > @10 exception:v36glovar.py [4:0] -> [5:17] [35=[open]]
11   v38 = invokestatic < PythonLoader, Lhasattr, import()Lhasattr; > @11 exception:v39glovar.py [4:0] -> [5:17] [38=[hasattr]]
12   v41 = invokestatic < PythonLoader, LBaseException, import()LBaseException; > @12 exception:v42glovar.py [4:0] -> [5:17] [41=[BaseException]]
13   v44 = invokestatic < PythonLoader, Labs, import()Labs; > @13 exception:v45glovar.py [4:0] -> [5:17] [44=[abs]]
14   v47 = invokestatic < PythonLoader, Lrange, import()Lrange; > @14 exception:v48glovar.py [4:0] -> [5:17] [47=[range]]
16   v51 = invokeFunction < PythonLoader, LCodeBody, do()LRoot; > v11,v52:#3.0 @16 exception:v53glovar.py [5:11] -> [5:17] [51=[globVar2]11=[int]]
BB2

while:

#glovar.py
def printVar():
	print globVar + globVar2

globVar = "Foo"
globVar2 = int(3)

Produces:

module code:
BB0
BB1
0   v5 = invokestatic < PythonLoader, Lstr, import()Lstr; > @0 exception:v6glovar.py [1:0] -> [5:17] [5=[str]]
1   v8 = invokestatic < PythonLoader, Lfloat, import()Lfloat; > @1 exception:v9glovar.py [1:0] -> [5:17] [8=[float]]
2   v11 = invokestatic < PythonLoader, Lint, import()Lint; > @2 exception:v12glovar.py [1:0] -> [5:17] [11=[int]]
3   v14 = invokestatic < PythonLoader, L__name__, import()L__name__; > @3 exception:v15glovar.py [1:0] -> [5:17] [14=[__name__]]
4   v17 = invokestatic < PythonLoader, LFalse, import()LFalse; > @4 exception:v18glovar.py [1:0] -> [5:17] [17=[False]]
5   v20 = invokestatic < PythonLoader, LTrue, import()LTrue; > @5 exception:v21glovar.py [1:0] -> [5:17] [20=[True]]
6   v23 = invokestatic < PythonLoader, LNone, import()LNone; > @6 exception:v24glovar.py [1:0] -> [5:17] [23=[None]]
7   v26 = invokestatic < PythonLoader, Lprint, import()Lprint; > @7 exception:v27glovar.py [1:0] -> [5:17] [26=[print]]
8   v29 = invokestatic < PythonLoader, Lsuper, import()Lsuper; > @8 exception:v30glovar.py [1:0] -> [5:17] [29=[super]]
9   v32 = invokestatic < PythonLoader, Llen, import()Llen; > @9 exception:v33glovar.py [1:0] -> [5:17] [32=[len]]
10   v35 = invokestatic < PythonLoader, Lopen, import()Lopen; > @10 exception:v36glovar.py [1:0] -> [5:17] [35=[open]]
11   v38 = invokestatic < PythonLoader, Lhasattr, import()Lhasattr; > @11 exception:v39glovar.py [1:0] -> [5:17] [38=[hasattr]]
12   v41 = invokestatic < PythonLoader, LBaseException, import()LBaseException; > @12 exception:v42glovar.py [1:0] -> [5:17] [41=[BaseException]]
13   v44 = invokestatic < PythonLoader, Labs, import()Labs; > @13 exception:v45glovar.py [1:0] -> [5:17] [44=[abs]]
14   v47 = invokestatic < PythonLoader, Lrange, import()Lrange; > @14 exception:v48glovar.py [1:0] -> [5:17] [47=[range]]
15   v50 = new <PythonLoader,Lscript glovar.py/printVar>@15glovar.py [1:0] -> [5:17] [50=[printVar]]
17   v55 = invokeFunction < PythonLoader, LCodeBody, do()LRoot; > v11,v56:#3.0 @17 exception:v57glovar.py [5:11] -> [5:14] [11=[int]]
18   global:global globVar2 = v55            glovar.py [5:0] -> [5:1]
BB2


printVar:
BB0
BB1
0   v6 = global:global globVar2              glovar.py [2:17] -> [2:25]
1   v3 = binaryop(add) v4:#Foo , v6glovar.py [2:7] -> [2:25]
BB2

They should always have an AstGlobalWrite instruction because they can be imported by other modules that import the one they are declared in.

An interesting thing I found is that if I alter the source code, changing the order of the functions, the IR produced is different:

#glovar.py
globVar = "Foo"
globVar2 = int(3)

def printVar():
	print globVar + globVar2

The IR it produces changes to:

module code:
BB0
BB1
0   v5 = invokestatic < PythonLoader, Lstr, import()Lstr; > @0 exception:v6glovar.py [1:0] -> [5:32] [5=[str]]
1   v8 = invokestatic < PythonLoader, Lfloat, import()Lfloat; > @1 exception:v9glovar.py [1:0] -> [5:32] [8=[float]]
2   v11 = invokestatic < PythonLoader, Lint, import()Lint; > @2 exception:v12glovar.py [1:0] -> [5:32] [11=[int]]
3   v14 = invokestatic < PythonLoader, L__name__, import()L__name__; > @3 exception:v15glovar.py [1:0] -> [5:32] [14=[__name__]]
4   v17 = invokestatic < PythonLoader, LFalse, import()LFalse; > @4 exception:v18glovar.py [1:0] -> [5:32] [17=[False]]
5   v20 = invokestatic < PythonLoader, LTrue, import()LTrue; > @5 exception:v21glovar.py [1:0] -> [5:32] [20=[True]]
6   v23 = invokestatic < PythonLoader, LNone, import()LNone; > @6 exception:v24glovar.py [1:0] -> [5:32] [23=[None]]
7   v26 = invokestatic < PythonLoader, Lprint, import()Lprint; > @7 exception:v27glovar.py [1:0] -> [5:32] [26=[print]]
8   v29 = invokestatic < PythonLoader, Lsuper, import()Lsuper; > @8 exception:v30glovar.py [1:0] -> [5:32] [29=[super]]
9   v32 = invokestatic < PythonLoader, Llen, import()Llen; > @9 exception:v33glovar.py [1:0] -> [5:32] [32=[len]]
10   v35 = invokestatic < PythonLoader, Lopen, import()Lopen; > @10 exception:v36glovar.py [1:0] -> [5:32] [35=[open]]
11   v38 = invokestatic < PythonLoader, Lhasattr, import()Lhasattr; > @11 exception:v39glovar.py [1:0] -> [5:32] [38=[hasattr]]
12   v41 = invokestatic < PythonLoader, LBaseException, import()LBaseException; > @12 exception:v42glovar.py [1:0] -> [5:32] [41=[BaseException]]
13   v44 = invokestatic < PythonLoader, Labs, import()Labs; > @13 exception:v45glovar.py [1:0] -> [5:32] [44=[abs]]
14   v47 = invokestatic < PythonLoader, Lrange, import()Lrange; > @14 exception:v48glovar.py [1:0] -> [5:32] [47=[range]]
16   v51 = invokeFunction < PythonLoader, LCodeBody, do()LRoot; > v11,v52:#3.0 @16 exception:v53glovar.py [2:11] -> [2:17] [51=[globVar2]11=[int]]
18   v56 = new <PythonLoader,Lscript glovar.py/printVar>@18glovar.py [1:0] -> [5:32] [56=[printVar]]
BB2


printVar:
BB0
BB1
0   v6 = lexical:globVar2@Lscript glovar.py  glovar.py [5:17] -> [5:25]
1   v3 = binaryop(add) v4:#Foo , v6          glovar.py [5:7] -> [5:25]
BB2

What is the reason the IR is different in this case?
Thanks in advance,
Sifis.

Edit: Another issue of the IR visible in these examples is the fact that print statements do not produce a method invocation.

@sifislag
Copy link
Author

Commits de848a5 and ebc0b14 should have fixed this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant