summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--compiler/optimizing/builder.cc5
-rw-r--r--compiler/optimizing/code_generator_arm.cc31
-rw-r--r--compiler/optimizing/code_generator_x86.cc39
-rw-r--r--compiler/optimizing/code_generator_x86_64.cc40
-rw-r--r--test/422-type-conversion/src/Main.java71
5 files changed, 177 insertions, 9 deletions
diff --git a/compiler/optimizing/builder.cc b/compiler/optimizing/builder.cc
index b51b6e7d25..b5aff4b576 100644
--- a/compiler/optimizing/builder.cc
+++ b/compiler/optimizing/builder.cc
@@ -1007,6 +1007,11 @@ bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32
break;
}
+ case Instruction::INT_TO_SHORT: {
+ Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort);
+ break;
+ }
+
case Instruction::INT_TO_CHAR: {
Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar);
break;
diff --git a/compiler/optimizing/code_generator_arm.cc b/compiler/optimizing/code_generator_arm.cc
index bb2f899886..c17142d7c9 100644
--- a/compiler/optimizing/code_generator_arm.cc
+++ b/compiler/optimizing/code_generator_arm.cc
@@ -1367,6 +1367,22 @@ void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
}
break;
+ case Primitive::kPrimShort:
+ switch (input_type) {
+ case Primitive::kPrimByte:
+ case Primitive::kPrimInt:
+ case Primitive::kPrimChar:
+ // Processing a Dex `int-to-short' instruction.
+ locations->SetInAt(0, Location::RequiresRegister());
+ locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
+ break;
+
+ default:
+ LOG(FATAL) << "Unexpected type conversion from " << input_type
+ << " to " << result_type;
+ }
+ break;
+
case Primitive::kPrimInt:
switch (input_type) {
case Primitive::kPrimLong:
@@ -1461,6 +1477,21 @@ void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversio
}
break;
+ case Primitive::kPrimShort:
+ switch (input_type) {
+ case Primitive::kPrimByte:
+ case Primitive::kPrimInt:
+ case Primitive::kPrimChar:
+ // Processing a Dex `int-to-short' instruction.
+ __ sbfx(out.As<Register>(), in.As<Register>(), 0, 16);
+ break;
+
+ default:
+ LOG(FATAL) << "Unexpected type conversion from " << input_type
+ << " to " << result_type;
+ }
+ break;
+
case Primitive::kPrimInt:
switch (input_type) {
case Primitive::kPrimLong:
diff --git a/compiler/optimizing/code_generator_x86.cc b/compiler/optimizing/code_generator_x86.cc
index 2ba2501f50..c486580216 100644
--- a/compiler/optimizing/code_generator_x86.cc
+++ b/compiler/optimizing/code_generator_x86.cc
@@ -1310,6 +1310,22 @@ void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
}
break;
+ case Primitive::kPrimShort:
+ switch (input_type) {
+ case Primitive::kPrimByte:
+ case Primitive::kPrimInt:
+ case Primitive::kPrimChar:
+ // Processing a Dex `int-to-short' instruction.
+ locations->SetInAt(0, Location::Any());
+ locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
+ break;
+
+ default:
+ LOG(FATAL) << "Unexpected type conversion from " << input_type
+ << " to " << result_type;
+ }
+ break;
+
case Primitive::kPrimInt:
switch (input_type) {
case Primitive::kPrimLong:
@@ -1412,6 +1428,29 @@ void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversio
}
break;
+ case Primitive::kPrimShort:
+ switch (input_type) {
+ case Primitive::kPrimByte:
+ case Primitive::kPrimInt:
+ case Primitive::kPrimChar:
+ // Processing a Dex `int-to-short' instruction.
+ if (in.IsRegister()) {
+ __ movsxw(out.As<Register>(), in.As<Register>());
+ } else if (in.IsStackSlot()) {
+ __ movsxw(out.As<Register>(), Address(ESP, in.GetStackIndex()));
+ } else {
+ DCHECK(in.GetConstant()->IsIntConstant());
+ int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
+ __ movl(out.As<Register>(), Immediate(static_cast<int16_t>(value)));
+ }
+ break;
+
+ default:
+ LOG(FATAL) << "Unexpected type conversion from " << input_type
+ << " to " << result_type;
+ }
+ break;
+
case Primitive::kPrimInt:
switch (input_type) {
case Primitive::kPrimLong:
diff --git a/compiler/optimizing/code_generator_x86_64.cc b/compiler/optimizing/code_generator_x86_64.cc
index 159b54690a..735addba4b 100644
--- a/compiler/optimizing/code_generator_x86_64.cc
+++ b/compiler/optimizing/code_generator_x86_64.cc
@@ -1304,6 +1304,22 @@ void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
}
break;
+ case Primitive::kPrimShort:
+ switch (input_type) {
+ case Primitive::kPrimByte:
+ case Primitive::kPrimInt:
+ case Primitive::kPrimChar:
+ // Processing a Dex `int-to-short' instruction.
+ locations->SetInAt(0, Location::Any());
+ locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
+ break;
+
+ default:
+ LOG(FATAL) << "Unexpected type conversion from " << input_type
+ << " to " << result_type;
+ }
+ break;
+
case Primitive::kPrimInt:
switch (input_type) {
case Primitive::kPrimLong:
@@ -1409,6 +1425,30 @@ void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conver
}
break;
+ case Primitive::kPrimShort:
+ switch (input_type) {
+ case Primitive::kPrimByte:
+ case Primitive::kPrimInt:
+ case Primitive::kPrimChar:
+ // Processing a Dex `int-to-short' instruction.
+ if (in.IsRegister()) {
+ __ movsxw(out.As<CpuRegister>(), in.As<CpuRegister>());
+ } else if (in.IsStackSlot()) {
+ __ movsxw(out.As<CpuRegister>(),
+ Address(CpuRegister(RSP), in.GetStackIndex()));
+ } else {
+ DCHECK(in.GetConstant()->IsIntConstant());
+ __ movl(out.As<CpuRegister>(),
+ Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
+ }
+ break;
+
+ default:
+ LOG(FATAL) << "Unexpected type conversion from " << input_type
+ << " to " << result_type;
+ }
+ break;
+
case Primitive::kPrimInt:
switch (input_type) {
case Primitive::kPrimLong:
diff --git a/test/422-type-conversion/src/Main.java b/test/422-type-conversion/src/Main.java
index 88b45280ac..0bf958541b 100644
--- a/test/422-type-conversion/src/Main.java
+++ b/test/422-type-conversion/src/Main.java
@@ -24,6 +24,12 @@ public class Main {
}
}
+ public static void assertShortEquals(short expected, short result) {
+ if (expected != result) {
+ throw new Error("Expected: " + expected + ", found: " + result);
+ }
+ }
+
public static void assertIntEquals(int expected, int result) {
if (expected != result) {
throw new Error("Expected: " + expected + ", found: " + result);
@@ -39,23 +45,32 @@ public class Main {
public static void assertCharEquals(char expected, char result) {
if (expected != result) {
// Values are cast to int to display numeric values instead of
- // (Unicode) characters.
+ // (UTF-16 encoded) characters.
throw new Error("Expected: " + (int)expected + ", found: " + (int)result);
}
}
public static void main(String[] args) {
+ // Generate, compile and check int-to-long Dex instructions.
byteToLong();
shortToLong();
intToLong();
charToLong();
+ // Generate, compile and check long-to-int Dex instructions.
longToInt();
+ // Generate, compile and check int-to-byte Dex instructions.
shortToByte();
intToByte();
charToByte();
+ // Generate, compile and check int-to-short Dex instructions.
+ byteToShort();
+ intToShort();
+ charToShort();
+
+ // Generate, compile and check int-to-char Dex instructions.
byteToChar();
shortToChar();
intToChar();
@@ -100,10 +115,6 @@ public class Main {
assertLongEquals(51L, $opt$CharToLong((char)51));
assertLongEquals(32767L, $opt$CharToLong((char)32767)); // 2^15 - 1
assertLongEquals(65535L, $opt$CharToLong((char)65535)); // 2^16 - 1
-
- assertLongEquals(0L, $opt$CharToLong('\u0000'));
- assertLongEquals(65535L, $opt$CharToLong('\uFFFF')); // 2^16 - 1
-
assertLongEquals(65535L, $opt$CharToLong((char)-1));
assertLongEquals(65485L, $opt$CharToLong((char)-51));
assertLongEquals(32769L, $opt$CharToLong((char)-32767)); // -(2^15 - 1)
@@ -175,10 +186,6 @@ public class Main {
assertByteEquals((byte)-128, $opt$CharToByte((char)128)); // 2^7
assertByteEquals((byte)-1, $opt$CharToByte((char)32767)); // 2^15 - 1
assertByteEquals((byte)-1, $opt$CharToByte((char)65535)); // 2^16 - 1
-
- assertByteEquals((byte)0, $opt$CharToByte('\u0000'));
- assertByteEquals((byte)-1, $opt$CharToByte('\uFFFF')); // 2^16 - 1
-
assertByteEquals((byte)-1, $opt$CharToByte((char)-1));
assertByteEquals((byte)-51, $opt$CharToByte((char)-51));
assertByteEquals((byte)-127, $opt$CharToByte((char)-127)); // -(2^7 - 1)
@@ -186,6 +193,47 @@ public class Main {
assertByteEquals((byte)127, $opt$CharToByte((char)-129)); // -(2^7 + 1)
}
+ private static void byteToShort() {
+ assertShortEquals((short)1, $opt$ByteToShort((byte)1));
+ assertShortEquals((short)0, $opt$ByteToShort((byte)0));
+ assertShortEquals((short)-1, $opt$ByteToShort((byte)-1));
+ assertShortEquals((short)51, $opt$ByteToShort((byte)51));
+ assertShortEquals((short)-51, $opt$ByteToShort((byte)-51));
+ assertShortEquals((short)127, $opt$ByteToShort((byte)127)); // 2^7 - 1
+ assertShortEquals((short)-127, $opt$ByteToShort((byte)-127)); // -(2^7 - 1)
+ assertShortEquals((short)-128, $opt$ByteToShort((byte)-128)); // -(2^7)
+ }
+
+ private static void intToShort() {
+ assertShortEquals((short)1, $opt$IntToShort(1));
+ assertShortEquals((short)0, $opt$IntToShort(0));
+ assertShortEquals((short)-1, $opt$IntToShort(-1));
+ assertShortEquals((short)51, $opt$IntToShort(51));
+ assertShortEquals((short)-51, $opt$IntToShort(-51));
+ assertShortEquals((short)32767, $opt$IntToShort(32767)); // 2^15 - 1
+ assertShortEquals((short)-32767, $opt$IntToShort(-32767)); // -(2^15 - 1)
+ assertShortEquals((short)-32768, $opt$IntToShort(-32768)); // -(2^15)
+ assertShortEquals((short)-32768, $opt$IntToShort(32768)); // 2^15
+ assertShortEquals((short)32767, $opt$IntToShort(-32769)); // -(2^15 + 1)
+ assertShortEquals((short)-1, $opt$IntToShort(2147483647)); // 2^31 - 1
+ assertShortEquals((short)0, $opt$IntToShort(-2147483648)); // -(2^31)
+ }
+
+ private static void charToShort() {
+ assertShortEquals((short)1, $opt$CharToShort((char)1));
+ assertShortEquals((short)0, $opt$CharToShort((char)0));
+ assertShortEquals((short)51, $opt$CharToShort((char)51));
+ assertShortEquals((short)32767, $opt$CharToShort((char)32767)); // 2^15 - 1
+ assertShortEquals((short)-32768, $opt$CharToShort((char)32768)); // 2^15
+ assertShortEquals((short)-32767, $opt$CharToShort((char)32769)); // 2^15
+ assertShortEquals((short)-1, $opt$CharToShort((char)65535)); // 2^16 - 1
+ assertShortEquals((short)-1, $opt$CharToShort((char)-1));
+ assertShortEquals((short)-51, $opt$CharToShort((char)-51));
+ assertShortEquals((short)-32767, $opt$CharToShort((char)-32767)); // -(2^15 - 1)
+ assertShortEquals((short)-32768, $opt$CharToShort((char)-32768)); // -(2^15)
+ assertShortEquals((short)32767, $opt$CharToShort((char)-32769)); // -(2^15 + 1)
+ }
+
private static void byteToChar() {
assertCharEquals((char)1, $opt$ByteToChar((byte)1));
assertCharEquals((char)0, $opt$ByteToChar((byte)0));
@@ -242,6 +290,11 @@ public class Main {
static byte $opt$IntToByte(int a){ return (byte)a; }
static byte $opt$CharToByte(char a){ return (byte)a; }
+ // These methods produce int-to-short Dex instructions.
+ static short $opt$ByteToShort(byte a){ return (short)a; }
+ static short $opt$IntToShort(int a){ return (short)a; }
+ static short $opt$CharToShort(char a){ return (short)a; }
+
// These methods produce int-to-char Dex instructions.
static char $opt$ByteToChar(byte a){ return (char)a; }
static char $opt$ShortToChar(short a){ return (char)a; }