[llvm-commits] [dragonegg] r128541 - /dragonegg/trunk/Constants.cpp

Duncan Sands baldrick at free.fr
Wed Mar 30 07:38:25 PDT 2011


Author: baldrick
Date: Wed Mar 30 09:38:25 2011
New Revision: 128541

URL: http://llvm.org/viewvc/llvm-project?rev=128541&view=rev
Log:
The Java front-end expects array constructor elements to be implicitly
cast to the array element type.  For example, it may supply i32 values
for an array of i16.  With this fix dragonegg can compile a Java "hello
world" program.

Modified:
    dragonegg/trunk/Constants.cpp

Modified: dragonegg/trunk/Constants.cpp
URL: http://llvm.org/viewvc/llvm-project/dragonegg/trunk/Constants.cpp?rev=128541&r1=128540&r2=128541&view=diff
==============================================================================
--- dragonegg/trunk/Constants.cpp (original)
+++ dragonegg/trunk/Constants.cpp Wed Mar 30 09:38:25 2011
@@ -578,6 +578,10 @@
 
   // If we have a lower bound for the range of the type, get it.
   tree init_type = TREE_TYPE(exp);
+  tree elt_type = TREE_TYPE(init_type);
+  const Type *EltTy = ConvertType(elt_type);
+  bool EltIsSigned = !TYPE_UNSIGNED(elt_type);
+
   tree min_element = size_zero_node;
   std::vector<Constant*> ResultElts;
 
@@ -606,6 +610,18 @@
   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), ix, elt_index, elt_value) {
     // Find and decode the constructor's value.
     Constant *Val = ConvertInitializer(elt_value);
+
+    // If needed, cast the value to the type of the array element.
+    if (TREE_TYPE(elt_value) != elt_type && !AGGREGATE_TYPE_P(elt_type) &&
+        !AGGREGATE_TYPE_P(TREE_TYPE(elt_value))) {
+      const Type *ValTy = ConvertType(TREE_TYPE(elt_value));
+      Val = InterpretAsType(Val, ValTy, 0);
+      bool ValIsSigned = !TYPE_UNSIGNED(TREE_TYPE(elt_value));
+      Instruction::CastOps opcode = CastInst::getCastOpcode(Val, ValIsSigned,
+                                                            EltTy, EltIsSigned);
+      Val = TheFolder->CreateCast(opcode, Val, EltTy);
+    }
+
     SomeVal = Val;
 
     // Get the index position of the element within the array.  Note that this





More information about the llvm-commits mailing list