Skip to content

Commit e93de8e

Browse files
committed
Refactor getattribute function node
1 parent cb39c35 commit e93de8e

File tree

2 files changed

+37
-70
lines changed

2 files changed

+37
-70
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java

Lines changed: 26 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,9 @@
115115
import com.oracle.graal.python.builtins.Python3Core;
116116
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
117117
import com.oracle.graal.python.builtins.PythonBuiltins;
118-
import com.oracle.graal.python.builtins.modules.WarningsModuleBuiltins.WarnNode;
119-
import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory.GetAttrNodeFactory;
120118
import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory.HexNodeFactory;
121119
import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory.OctNodeFactory;
120+
import com.oracle.graal.python.builtins.modules.WarningsModuleBuiltins.WarnNode;
122121
import com.oracle.graal.python.builtins.modules.ast.AstModuleBuiltins;
123122
import com.oracle.graal.python.builtins.modules.io.IOModuleBuiltins;
124123
import com.oracle.graal.python.builtins.modules.io.IONodes;
@@ -194,8 +193,6 @@
194193
import com.oracle.graal.python.nodes.argument.ReadArgumentNode;
195194
import com.oracle.graal.python.nodes.attributes.DeleteAttributeNode;
196195
import com.oracle.graal.python.nodes.attributes.GetAttributeNode;
197-
import com.oracle.graal.python.nodes.attributes.GetAttributeNode.GetAnyAttributeNode;
198-
import com.oracle.graal.python.nodes.attributes.GetAttributeNode.GetFixedAttributeNode;
199196
import com.oracle.graal.python.nodes.attributes.LookupCallableSlotInMRONode;
200197
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
201198
import com.oracle.graal.python.nodes.attributes.SetAttributeNode;
@@ -1304,71 +1301,41 @@ Object delattr(VirtualFrame frame, Object object, Object name) {
13041301
// getattr(object, name[, default])
13051302
@Builtin(name = J_GETATTR, minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 3)
13061303
@GenerateNodeFactory
1307-
public abstract static class GetAttrNode extends PythonTernaryBuiltinNode {
1308-
@NeverDefault
1309-
public static GetAttrNode create() {
1310-
return GetAttrNodeFactory.create();
1311-
}
1312-
1313-
public abstract Object executeWithArgs(VirtualFrame frame, Object primary, TruffleString name, Object defaultValue);
1304+
abstract static class GetAttrNode extends PythonTernaryBuiltinNode {
13141305

1315-
@SuppressWarnings({"unused", "truffle-static-method"})
1316-
@Specialization(guards = {"stringEquals(cachedName, name, equalNode, inliningTarget, stringProfile)", "isNoValue(defaultValue)"})
1317-
public Object getAttrDefault(VirtualFrame frame, Object primary, TruffleString name, PNone defaultValue,
1318-
@Bind("this") Node inliningTarget,
1319-
@Shared @Cached InlinedConditionProfile stringProfile,
1320-
@Shared @Cached TruffleString.EqualNode equalNode,
1321-
@Shared @Cached("name") TruffleString cachedName,
1322-
@Cached("create(name)") GetFixedAttributeNode getAttributeNode) {
1323-
return getAttributeNode.executeObject(frame, primary);
1306+
@Specialization(guards = "isNoValue(defaultValue)")
1307+
static Object getAttrNoDefault(VirtualFrame frame, Object primary, Object nameObj, @SuppressWarnings("unused") Object defaultValue,
1308+
@Shared @Cached CastToTruffleStringNode cast,
1309+
@Shared @Cached PRaiseNode raiseNode,
1310+
@Cached PyObjectGetAttr getAttr) {
1311+
TruffleString name = castName(nameObj, cast, raiseNode);
1312+
return getAttr.execute(frame, primary, name);
13241313
}
13251314

1326-
@SuppressWarnings({"unused", "truffle-static-method"})
1327-
@Specialization(guards = {"stringEquals(cachedName, name, equalNode, inliningTarget, stringProfile)", "!isNoValue(defaultValue)"})
1328-
Object getAttr(VirtualFrame frame, Object primary, TruffleString name, Object defaultValue,
1315+
@Specialization(guards = "!isNoValue(defaultValue)")
1316+
static Object getAttrWithDefault(VirtualFrame frame, Object primary, Object nameObj, Object defaultValue,
13291317
@Bind("this") Node inliningTarget,
1330-
@Shared @Cached InlinedConditionProfile stringProfile,
1331-
@Shared @Cached TruffleString.EqualNode equalNode,
1332-
@Shared @Cached("name") TruffleString cachedName,
1333-
@Cached("create(name)") GetFixedAttributeNode getAttributeNode,
1334-
@Shared @Cached IsBuiltinObjectProfile errorProfile) {
1335-
try {
1336-
return getAttributeNode.executeObject(frame, primary);
1337-
} catch (PException e) {
1338-
e.expectAttributeError(inliningTarget, errorProfile);
1318+
@Shared @Cached CastToTruffleStringNode cast,
1319+
@Shared @Cached PRaiseNode raiseNode,
1320+
@Cached InlinedConditionProfile noValueProfile,
1321+
@Cached PyObjectLookupAttr lookupAttr) {
1322+
TruffleString name = castName(nameObj, cast, raiseNode);
1323+
Object result = lookupAttr.execute(frame, primary, name);
1324+
if (noValueProfile.profile(inliningTarget, result == NO_VALUE)) {
13391325
return defaultValue;
1326+
} else {
1327+
return result;
13401328
}
13411329
}
13421330

1343-
@Specialization(replaces = {"getAttr", "getAttrDefault"}, guards = "isNoValue(defaultValue)")
1344-
Object getAttrFromObject(VirtualFrame frame, Object primary, TruffleString name, @SuppressWarnings("unused") PNone defaultValue,
1345-
@Shared @Cached GetAnyAttributeNode getAttributeNode) {
1346-
return getAttributeNode.executeObject(frame, primary, name);
1347-
}
1348-
1349-
@Specialization(replaces = {"getAttr", "getAttrDefault"}, guards = "!isNoValue(defaultValue)")
1350-
@SuppressWarnings("truffle-static-method")
1351-
Object getAttrFromObject(VirtualFrame frame, Object primary, TruffleString name, Object defaultValue,
1352-
@Bind("this") Node inliningTarget,
1353-
@Shared @Cached GetAnyAttributeNode getAttributeNode,
1354-
@Shared @Cached IsBuiltinObjectProfile errorProfile) {
1331+
private static TruffleString castName(Object nameObj, CastToTruffleStringNode cast, PRaiseNode raiseNode) {
1332+
TruffleString name;
13551333
try {
1356-
return getAttributeNode.executeObject(frame, primary, name);
1357-
} catch (PException e) {
1358-
e.expectAttributeError(inliningTarget, errorProfile);
1359-
return defaultValue;
1334+
name = cast.execute(nameObj);
1335+
} catch (CannotCastException e) {
1336+
throw raiseNode.raise(TypeError, ErrorMessages.GETATTR_ATTRIBUTE_NAME_MUST_BE_STRING);
13601337
}
1361-
}
1362-
1363-
@Specialization
1364-
Object getAttr2(VirtualFrame frame, Object object, PString name, Object defaultValue) {
1365-
return executeWithArgs(frame, object, name.getValueUncached(), defaultValue);
1366-
}
1367-
1368-
@Specialization(guards = "!isString(name)")
1369-
@SuppressWarnings("unused")
1370-
Object getAttrGeneric(Object primary, Object name, Object defaultValue) {
1371-
throw raise(TypeError, ErrorMessages.GETATTR_ATTRIBUTE_NAME_MUST_BE_STRING);
1338+
return name;
13721339
}
13731340
}
13741341

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CExtNodes.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
import com.oracle.graal.python.PythonLanguage;
7070
import com.oracle.graal.python.builtins.Python3Core;
7171
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
72-
import com.oracle.graal.python.builtins.modules.BuiltinFunctions.GetAttrNode;
7372
import com.oracle.graal.python.builtins.objects.PNone;
7473
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
7574
import com.oracle.graal.python.builtins.objects.bytes.PByteArray;
@@ -141,6 +140,7 @@
141140
import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsTypeNode;
142141
import com.oracle.graal.python.builtins.objects.type.TypeNodes.ProfileClassNode;
143142
import com.oracle.graal.python.lib.PyFloatAsDoubleNode;
143+
import com.oracle.graal.python.lib.PyObjectGetAttr;
144144
import com.oracle.graal.python.lib.PyObjectLookupAttr;
145145
import com.oracle.graal.python.lib.PyObjectSizeNode;
146146
import com.oracle.graal.python.nodes.BuiltinNames;
@@ -1251,56 +1251,56 @@ public final Object execute(VirtualFrame frame, Object[] args) {
12511251
Object upcall0(VirtualFrame frame, Object[] args,
12521252
@Cached CallNode callNode,
12531253
@Cached NativeToPythonNode receiverToJavaNode,
1254-
@Shared("getAttrNode") @Cached GetAttrNode getAttrNode) {
1254+
@Shared("getAttrNode") @Cached PyObjectGetAttr getAttrNode) {
12551255
Object receiver = receiverToJavaNode.execute(args[0]);
12561256
assert PGuards.isString(args[1]);
1257-
Object callable = getAttrNode.execute(frame, receiver, args[1], PNone.NO_VALUE);
1257+
Object callable = getAttrNode.execute(frame, receiver, args[1]);
12581258
return callNode.execute(frame, callable, PythonUtils.EMPTY_OBJECT_ARRAY, PKeyword.EMPTY_KEYWORDS);
12591259
}
12601260

12611261
@Specialization(guards = "args.length == 3")
12621262
Object upcall1(VirtualFrame frame, Object[] args,
12631263
@Cached CallUnaryMethodNode callNode,
12641264
@Cached NativeToPythonNode receiverToJavaNode,
1265-
@Shared("getAttrNode") @Cached GetAttrNode getAttrNode) {
1265+
@Shared("getAttrNode") @Cached PyObjectGetAttr getAttrNode) {
12661266
Object receiver = receiverToJavaNode.execute(args[0]);
12671267
assert PGuards.isString(args[1]);
1268-
Object callable = getAttrNode.execute(frame, receiver, args[1], PNone.NO_VALUE);
1268+
Object callable = getAttrNode.execute(frame, receiver, args[1]);
12691269
return callNode.executeObject(frame, callable, args[2]);
12701270
}
12711271

12721272
@Specialization(guards = "args.length == 4")
12731273
Object upcall2(VirtualFrame frame, Object[] args,
12741274
@Cached CallBinaryMethodNode callNode,
12751275
@Cached NativeToPythonNode receiverToJavaNode,
1276-
@Shared("getAttrNode") @Cached GetAttrNode getAttrNode) {
1276+
@Shared("getAttrNode") @Cached PyObjectGetAttr getAttrNode) {
12771277
Object receiver = receiverToJavaNode.execute(args[0]);
12781278
assert PGuards.isString(args[1]);
1279-
Object callable = getAttrNode.execute(frame, receiver, args[1], PNone.NO_VALUE);
1279+
Object callable = getAttrNode.execute(frame, receiver, args[1]);
12801280
return callNode.executeObject(frame, callable, args[2], args[3]);
12811281
}
12821282

12831283
@Specialization(guards = "args.length == 5")
12841284
Object upcall3(VirtualFrame frame, Object[] args,
12851285
@Cached CallTernaryMethodNode callNode,
12861286
@Cached NativeToPythonNode receiverToJavaNode,
1287-
@Shared("getAttrNode") @Cached GetAttrNode getAttrNode) {
1287+
@Shared("getAttrNode") @Cached PyObjectGetAttr getAttrNode) {
12881288
Object receiver = receiverToJavaNode.execute(args[0]);
12891289
assert PGuards.isString(args[1]);
1290-
Object callable = getAttrNode.execute(frame, receiver, args[1], PNone.NO_VALUE);
1290+
Object callable = getAttrNode.execute(frame, receiver, args[1]);
12911291
return callNode.execute(frame, callable, args[2], args[3], args[4]);
12921292
}
12931293

12941294
@Specialization(replaces = {"upcall0", "upcall1", "upcall2", "upcall3"})
12951295
Object upcall(VirtualFrame frame, Object[] args,
12961296
@Cached CallNode callNode,
12971297
@Cached NativeToPythonNode receiverToJavaNode,
1298-
@Shared("getAttrNode") @Cached GetAttrNode getAttrNode) {
1298+
@Shared("getAttrNode") @Cached PyObjectGetAttr getAttrNode) {
12991299
// we needs at least a receiver and a member name
13001300
assert args.length >= 2;
13011301
Object receiver = receiverToJavaNode.execute(args[0]);
13021302
assert PGuards.isString(args[1]);
1303-
Object callable = getAttrNode.execute(frame, receiver, args[1], PNone.NO_VALUE);
1303+
Object callable = getAttrNode.execute(frame, receiver, args[1]);
13041304
return callNode.execute(frame, callable, Arrays.copyOfRange(args, 2, args.length), PKeyword.EMPTY_KEYWORDS);
13051305
}
13061306
}

0 commit comments

Comments
 (0)