forked from prestodb/presto
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add byte code expressions for arrays
- Loading branch information
Showing
11 changed files
with
586 additions
and
73 deletions.
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
presto-main/src/main/java/com/facebook/presto/byteCode/ArrayOpCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.facebook.presto.byteCode; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import java.util.Map; | ||
|
||
import static com.facebook.presto.byteCode.ArrayOpCode.AType.T_BOOLEAN; | ||
import static com.facebook.presto.byteCode.ArrayOpCode.AType.T_BYTE; | ||
import static com.facebook.presto.byteCode.ArrayOpCode.AType.T_CHAR; | ||
import static com.facebook.presto.byteCode.ArrayOpCode.AType.T_DOUBLE; | ||
import static com.facebook.presto.byteCode.ArrayOpCode.AType.T_FLOAT; | ||
import static com.facebook.presto.byteCode.ArrayOpCode.AType.T_INT; | ||
import static com.facebook.presto.byteCode.ArrayOpCode.AType.T_LONG; | ||
import static com.facebook.presto.byteCode.ArrayOpCode.AType.T_SHORT; | ||
import static com.facebook.presto.byteCode.OpCode.AALOAD; | ||
import static com.facebook.presto.byteCode.OpCode.AASTORE; | ||
import static com.facebook.presto.byteCode.OpCode.BALOAD; | ||
import static com.facebook.presto.byteCode.OpCode.BASTORE; | ||
import static com.facebook.presto.byteCode.OpCode.CALOAD; | ||
import static com.facebook.presto.byteCode.OpCode.CASTORE; | ||
import static com.facebook.presto.byteCode.OpCode.DALOAD; | ||
import static com.facebook.presto.byteCode.OpCode.DASTORE; | ||
import static com.facebook.presto.byteCode.OpCode.FALOAD; | ||
import static com.facebook.presto.byteCode.OpCode.FASTORE; | ||
import static com.facebook.presto.byteCode.OpCode.IALOAD; | ||
import static com.facebook.presto.byteCode.OpCode.IASTORE; | ||
import static com.facebook.presto.byteCode.OpCode.LALOAD; | ||
import static com.facebook.presto.byteCode.OpCode.LASTORE; | ||
import static com.facebook.presto.byteCode.OpCode.SALOAD; | ||
import static com.facebook.presto.byteCode.OpCode.SASTORE; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public enum ArrayOpCode | ||
{ | ||
NOT_PRIMITIVE(null, AALOAD, AASTORE, null), | ||
BYTE(byte.class, BALOAD, BASTORE, T_BYTE), | ||
BOOLEAN(boolean.class, BALOAD, BASTORE, T_BOOLEAN), | ||
CHAR(char.class, CALOAD, CASTORE, T_CHAR), | ||
SHORT(short.class, SALOAD, SASTORE, T_SHORT), | ||
INT(int.class, IALOAD, IASTORE, T_INT), | ||
LONG(long.class, LALOAD, LASTORE, T_LONG), | ||
FLOAT(float.class, FALOAD, FASTORE, T_FLOAT), | ||
DOUBLE(double.class, DALOAD, DASTORE, T_DOUBLE); | ||
|
||
private final OpCode load; | ||
private final OpCode store; | ||
private final AType atype; | ||
private final Class<?> type; | ||
|
||
private static final Map<Class<?>, ArrayOpCode> arrayOpCodeMap = initializeArrayOpCodeMap(); | ||
|
||
ArrayOpCode(@Nullable Class<?> clazz, OpCode load, OpCode store, @Nullable AType atype) | ||
{ | ||
this.type = clazz; | ||
this.load = requireNonNull(load, "load is null"); | ||
this.store = requireNonNull(store, "store is null"); | ||
this.atype = atype; | ||
} | ||
|
||
public OpCode getLoad() | ||
{ | ||
return load; | ||
} | ||
|
||
public OpCode getStore() | ||
{ | ||
return store; | ||
} | ||
|
||
public int getAtype() | ||
{ | ||
return atype.getType(); | ||
} | ||
|
||
public Class<?> getType() | ||
{ | ||
return type; | ||
} | ||
|
||
public static ArrayOpCode getArrayOpCode(ParameterizedType type) | ||
{ | ||
if (!type.isPrimitive()) { | ||
return NOT_PRIMITIVE; | ||
} | ||
ArrayOpCode arrayOpCode = arrayOpCodeMap.get(type.getPrimitiveType()); | ||
if (arrayOpCode == null) { | ||
throw new IllegalArgumentException("unsupported primitive type " + type); | ||
} | ||
return arrayOpCode; | ||
} | ||
|
||
static Map<Class<?>, ArrayOpCode> initializeArrayOpCodeMap() | ||
{ | ||
ImmutableMap.Builder<Class<?>, ArrayOpCode> builder = ImmutableMap.<Class<?>, ArrayOpCode>builder(); | ||
for (ArrayOpCode arrayOpCode : values()) { | ||
if (arrayOpCode.getType() != null) { | ||
builder.put(arrayOpCode.getType(), arrayOpCode); | ||
} | ||
} | ||
return builder.build(); | ||
} | ||
|
||
enum AType | ||
{ | ||
T_BOOLEAN(4), | ||
T_CHAR(5), | ||
T_FLOAT(6), | ||
T_DOUBLE(7), | ||
T_BYTE(8), | ||
T_SHORT(9), | ||
T_INT(10), | ||
T_LONG(11); | ||
|
||
private final int type; | ||
|
||
AType(int type) | ||
{ | ||
this.type = type; | ||
} | ||
|
||
int getType() | ||
{ | ||
return type; | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
.../src/main/java/com/facebook/presto/byteCode/expression/ArrayLengthByteCodeExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.facebook.presto.byteCode.expression; | ||
|
||
import com.facebook.presto.byteCode.ByteCodeBlock; | ||
import com.facebook.presto.byteCode.ByteCodeNode; | ||
import com.facebook.presto.byteCode.MethodGenerationContext; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
import java.util.List; | ||
|
||
import static com.facebook.presto.byteCode.OpCode.ARRAYLENGTH; | ||
import static com.facebook.presto.byteCode.ParameterizedType.type; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
class ArrayLengthByteCodeExpression | ||
extends ByteCodeExpression | ||
{ | ||
private final ByteCodeExpression instance; | ||
|
||
public ArrayLengthByteCodeExpression(ByteCodeExpression instance) | ||
{ | ||
super(type(int.class)); | ||
this.instance = requireNonNull(instance, "instance is null"); | ||
} | ||
|
||
@Override | ||
public ByteCodeNode getByteCode(MethodGenerationContext generationContext) | ||
{ | ||
return new ByteCodeBlock() | ||
.append(instance.getByteCode(generationContext)) | ||
.append(ARRAYLENGTH); | ||
} | ||
|
||
@Override | ||
protected String formatOneLine() | ||
{ | ||
return instance + ".length"; | ||
} | ||
|
||
@Override | ||
public List<ByteCodeNode> getChildNodes() | ||
{ | ||
return ImmutableList.<ByteCodeNode>of(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.