Skip to content

Commit 6399db7

Browse files
committed
add co_names and co_consts to code object
1 parent a4fa46c commit 6399db7

File tree

9 files changed

+182
-62
lines changed

9 files changed

+182
-62
lines changed

src/org/python/compiler/CodeCompiler.java

+3-11
Original file line numberDiff line numberDiff line change
@@ -2724,13 +2724,7 @@ public Object visitClassDef(ClassDef node) throws Exception {
27242724

27252725
@Override
27262726
public Object visitNum(Num node) throws Exception {
2727-
if (node.getInternalN() instanceof PyInteger || node.getInternalN() instanceof PyLong) {
2728-
module.longConstant(((PyObject)node.getInternalN()).__str__().toString()).get(code);
2729-
} else if (node.getInternalN() instanceof PyFloat) {
2730-
module.floatConstant(((PyFloat)node.getInternalN()).getValue()).get(code);
2731-
} else if (node.getInternalN() instanceof PyComplex) {
2732-
module.complexConstant(((PyComplex)node.getInternalN()).imag).get(code);
2733-
}
2727+
module.constant(node).get(code);
27342728
return null;
27352729
}
27362730

@@ -2884,15 +2878,13 @@ public Object visitName(Name node) throws Exception {
28842878

28852879
@Override
28862880
public Object visitBytes(Bytes node) throws Exception {
2887-
String s = node.getInternalS();
2888-
module.stringConstant(s).get(code);
2881+
module.constant(node).get(code);
28892882
return null;
28902883
}
28912884

28922885
@Override
28932886
public Object visitStr(Str node) throws Exception {
2894-
PyUnicode s = (PyUnicode)node.getInternalS();
2895-
module.unicodeConstant(s.asString()).get(code);
2887+
module.constant(node).get(code);
28962888
return null;
28972889
}
28982890

src/org/python/compiler/Module.java

+89-21
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
11
// Copyright (c) Corporation for National Research Initiatives
22
package org.python.compiler;
33

4-
import static org.python.util.CodegenUtils.ci;
5-
import static org.python.util.CodegenUtils.p;
6-
import static org.python.util.CodegenUtils.sig;
7-
8-
import java.io.IOException;
9-
import java.io.OutputStream;
10-
import java.util.ArrayList;
11-
import java.util.Enumeration;
12-
import java.util.Hashtable;
13-
import java.util.List;
14-
154
import org.objectweb.asm.Label;
165
import org.objectweb.asm.Opcodes;
176
import org.objectweb.asm.Type;
187
import org.python.antlr.ParseException;
198
import org.python.antlr.PythonTree;
9+
import org.python.antlr.ast.Bytes;
2010
import org.python.antlr.ast.Num;
2111
import org.python.antlr.ast.Str;
2212
import org.python.antlr.ast.Suite;
@@ -41,6 +31,15 @@
4131
import org.python.core.PyUnicode;
4232
import org.python.core.ThreadState;
4333

34+
import java.io.IOException;
35+
import java.io.OutputStream;
36+
import java.util.ArrayList;
37+
import java.util.Enumeration;
38+
import java.util.Hashtable;
39+
import java.util.List;
40+
41+
import static org.python.util.CodegenUtils.*;
42+
4443
class PyIntegerConstant extends Constant implements ClassConstants, Opcodes {
4544

4645
final int value;
@@ -250,7 +249,9 @@ class PyCodeConstant extends Constant implements ClassConstants, Opcodes {
250249
final String co_name;
251250
final int argcount;
252251
final int kwonlyargcount;
252+
final List<String> varnames;
253253
final List<String> names;
254+
final List<PythonTree> constants;
254255
final int id;
255256
final int co_firstlineno;
256257
final boolean arglist, keywordlist;
@@ -304,11 +305,13 @@ class PyCodeConstant extends Constant implements ClassConstants, Opcodes {
304305

305306
// !classdef only
306307
if (!classBody) {
307-
names = toNameAr(scope.names, false);
308+
varnames = toNameAr(scope.names, false);
308309
} else {
309-
names = null;
310+
varnames = null;
310311
}
311312

313+
constants = scope.constants;
314+
names = toNameAr(scope.globalNames, true);
312315
cellvars = toNameAr(scope.cellvars, true);
313316
freevars = toNameAr(scope.freevars, true);
314317
jy_npurecell = scope.jy_npurecell;
@@ -371,15 +374,15 @@ void put(Code c) throws IOException {
371374
module.classfile.addField(name, ci(PyCode.class), access);
372375
c.iconst(argcount);
373376

374-
// Make all names
375-
int nameArray;
376-
if (names != null) {
377-
nameArray = CodeCompiler.makeStrings(c, names);
377+
// Make all var names
378+
int varNameArr;
379+
if (varnames != null) {
380+
varNameArr = CodeCompiler.makeStrings(c, varnames);
378381
} else { // classdef
379-
nameArray = CodeCompiler.makeStrings(c, null);
382+
varNameArr = CodeCompiler.makeStrings(c, null);
380383
}
381-
c.aload(nameArray);
382-
c.freeLocal(nameArray);
384+
c.aload(varNameArr);
385+
c.freeLocal(varNameArr);
383386
c.aload(1);
384387
c.ldc(co_name);
385388
c.iconst(co_firstlineno);
@@ -406,6 +409,19 @@ void put(Code c) throws IOException {
406409
c.aconst_null();
407410
}
408411

412+
if (names != null) {
413+
int strArray = CodeCompiler.makeStrings(c, names);
414+
c.aload(strArray);
415+
c.freeLocal(strArray);
416+
} else {
417+
c.aconst_null();
418+
}
419+
if (constants != null) {
420+
int constArr = module.makeConstArray(c, constants);
421+
c.aload(constArr);
422+
c.freeLocal(constArr);
423+
}
424+
409425
c.iconst(jy_npurecell);
410426
c.iconst(kwonlyargcount);
411427
c.iconst(moreflags);
@@ -415,7 +431,8 @@ void put(Code c) throws IOException {
415431
"newCode",
416432
sig(PyCode.class, Integer.TYPE, String[].class, String.class, String.class,
417433
Integer.TYPE, Boolean.TYPE, Boolean.TYPE, PyFunctionTable.class,
418-
Integer.TYPE, String[].class, String[].class, Integer.TYPE, Integer.TYPE, Integer.TYPE));
434+
Integer.TYPE, String[].class, String[].class, String[].class, PyObject[].class,
435+
Integer.TYPE, Integer.TYPE, Integer.TYPE));
419436
c.putstatic(module.classfile.name, name, ci(PyCode.class));
420437
}
421438
}
@@ -479,6 +496,26 @@ private Constant findConstant(Constant c) {
479496
return ret;
480497
}
481498

499+
Constant constant(PythonTree node) {
500+
if (node instanceof Num) {
501+
PyObject n = (PyObject) ((Num) node).getInternalN();
502+
if (n instanceof PyLong) {
503+
return longConstant(n.__str__().toString());
504+
} else if (n instanceof PyFloat) {
505+
return floatConstant(((PyFloat) n).getValue());
506+
} else if (n instanceof PyComplex) {
507+
return complexConstant(((PyComplex)n).imag);
508+
}
509+
} else if (node instanceof Str) {
510+
PyUnicode s = (PyUnicode)((Str) node).getInternalS();
511+
return unicodeConstant(s.asString());
512+
} else if (node instanceof Bytes) {
513+
String s = ((Bytes) node).getInternalS();
514+
return stringConstant(s);
515+
}
516+
throw new RuntimeException("unexpected constant: " + node.toString());
517+
}
518+
482519
Constant integerConstant(int value) {
483520
return findConstant(new PyIntegerConstant(value));
484521
}
@@ -660,6 +697,37 @@ public void error(String msg, boolean err, PythonTree node) throws Exception {
660697
throw new ParseException(msg, node);
661698
}
662699

700+
public int makeConstArray(Code code, java.util.List<? extends PythonTree> nodes) throws IOException {
701+
final int n;
702+
703+
if (nodes == null) {
704+
n = 0;
705+
} else {
706+
n = nodes.size();
707+
}
708+
709+
int array = code.getLocal(ci(PyObject[].class));
710+
if (n == 0) {
711+
code.getstatic(p(Py.class), "EmptyObjects", ci(PyObject[].class));
712+
code.astore(array);
713+
} else {
714+
code.iconst(n);
715+
code.anewarray(p(PyObject.class));
716+
code.astore(array);
717+
718+
for (int i = 0; i < n; i++) {
719+
constant(nodes.get(i)).get(code);
720+
code.aload(array);
721+
code.swap();
722+
code.iconst(i);
723+
code.swap();
724+
code.aastore();
725+
}
726+
}
727+
return array;
728+
}
729+
730+
663731
public static void compile(mod node, OutputStream ostream, String name, String filename,
664732
boolean linenumbers, boolean printResults, CompilerFlags cflags) throws Exception {
665733
compile(node, ostream, name, filename, linenumbers, printResults, cflags,

src/org/python/compiler/ScopeInfo.java

+20-17
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22

33
package org.python.compiler;
44

5-
import com.google.common.base.Joiner;
65
import org.python.antlr.PythonTree;
76

87
import java.util.ArrayList;
9-
import java.util.Collections;
108
import java.util.Enumeration;
119
import java.util.Hashtable;
1210
import java.util.LinkedHashMap;
1311
import java.util.List;
1412
import java.util.Map;
15-
import java.util.Vector;
1613

1714
public class ScopeInfo extends Object implements ScopeConstants {
1815

@@ -74,7 +71,7 @@ public ScopeInfo(String name, PythonTree node, int level, int kind,
7471
public ArgListCompiler ac;
7572

7673
public Map<String, SymInfo> tbl = new LinkedHashMap<String, SymInfo>();
77-
public Vector<String> names = new Vector<String>();
74+
public List<String> names = new ArrayList<>();
7875

7976
public int addNonlocal(String name) {
8077
SymInfo info = tbl.get(name);
@@ -105,7 +102,7 @@ public int addGlobal(String name) {
105102
public void addParam(String name) {
106103
//System.out.println("addParam " + name);
107104
tbl.put(name, new SymInfo(PARAM|BOUND,local++));
108-
names.addElement(name);
105+
names.add(name);
109106
}
110107

111108
public void markFromParam() {
@@ -132,13 +129,20 @@ public void addUsed(String name) {
132129
}
133130
}
134131

132+
public void addConst(PythonTree node) {
133+
constants.add(node);
134+
}
135+
135136
private final static Object PRESENT = new Object();
136137

137138
public Hashtable<String,Object> inner_free = new Hashtable<String,Object>();
138139

139-
public Vector<String> cellvars = new Vector<String>();
140-
141-
public Vector<String> jy_paramcells = new Vector<String>();
140+
// FIXME: names should be varnames, globalNames is names, for corresponding fields in code object
141+
public List<String> globalNames = new ArrayList<>();
142+
public List<PythonTree> constants = new ArrayList<>(); // co_consts
143+
public List<String> freevars = new ArrayList<>();
144+
public List<String> cellvars = new ArrayList<>();
145+
public List<String> jy_paramcells = new ArrayList<>();
142146

143147
public int jy_npurecell;
144148

@@ -153,7 +157,7 @@ public void cook(ScopeInfo up, int distance, CompilationContext ctxt) throws Exc
153157
this.up = up;
154158
this.distance = distance;
155159
boolean func = kind == FUNCSCOPE;
156-
Vector<String> purecells = new Vector<String>();
160+
List<String> purecells = new ArrayList<>();
157161
cell = 0;
158162
boolean some_inner_free = inner_free.size() > 0;
159163

@@ -170,10 +174,10 @@ public void cook(ScopeInfo up, int distance, CompilationContext ctxt) throws Exc
170174
if ((flags&NGLOBAL) == 0 && (flags&BOUND) != 0) {
171175
info.flags |= CELL;
172176
if ((info.flags&PARAM) != 0)
173-
jy_paramcells.addElement(name);
174-
cellvars.addElement(name);
177+
jy_paramcells.add(name);
178+
cellvars.add(name);
175179
info.env_index = cell++;
176-
if ((flags&PARAM) == 0) purecells.addElement(name);
180+
if ((flags&PARAM) == 0) purecells.add(name);
177181
continue;
178182
}
179183
} else {
@@ -191,7 +195,7 @@ public void cook(ScopeInfo up, int distance, CompilationContext ctxt) throws Exc
191195
if ((flags&(GLOBAL|PARAM|CELL)) == 0) {
192196
if ((flags&BOUND) != 0) { // ?? only func
193197
// System.err.println("local: "+name);
194-
names.addElement(name);
198+
names.add(name);
195199
info.locals_index = local++;
196200
continue;
197201
}
@@ -203,7 +207,7 @@ public void cook(ScopeInfo up, int distance, CompilationContext ctxt) throws Exc
203207
if ((jy_npurecell = purecells.size()) > 0) {
204208
int sz = purecells.size();
205209
for (int i = 0; i < sz; i++) {
206-
names.addElement(purecells.elementAt(i));
210+
names.add(purecells.elementAt(i));
207211
}
208212
}
209213

@@ -241,8 +245,6 @@ private void dynastuff_trouble(boolean inner_free, CompilationContext ctxt) thro
241245
ctxt.error(illegal.toString(), true, scope_node);
242246
}
243247

244-
public Vector<String> freevars = new Vector<String>();
245-
246248
/**
247249
* setup the closure on this scope using the scope passed into cook as up as
248250
* the containing scope
@@ -270,7 +272,7 @@ public void setup_closure(ScopeInfo up){
270272
int up_flags = up_info.flags;
271273
if ((up_flags&(CELL|FREE)) != 0) {
272274
info.env_index = free++;
273-
freevars.addElement(name);
275+
freevars.add(name);
274276
continue;
275277
}
276278
// ! func global affect nested scopes
@@ -279,6 +281,7 @@ public void setup_closure(ScopeInfo up){
279281
continue;
280282
}
281283
}
284+
globalNames.add(name);
282285
info.flags &= ~FREE;
283286
}
284287
}

src/org/python/compiler/ScopesCompiler.java

+19
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,25 @@ public Object visitClassDef(ClassDef node) throws Exception {
322322
return null;
323323
}
324324

325+
@Override
326+
public Object visitNum(Num num) throws Exception {
327+
cur.addConst(num);
328+
return null;
329+
}
330+
331+
@Override
332+
public Object visitStr(Str s) throws Exception {
333+
cur.addConst(s);
334+
return null;
335+
}
336+
337+
@Override
338+
public Object visitBytes(Bytes b) throws Exception {
339+
cur.addConst(b);
340+
return null;
341+
}
342+
343+
325344
@Override
326345
public Object visitName(Name node) throws Exception {
327346
String name = node.getInternalId();

0 commit comments

Comments
 (0)