[llvm-commits] [hlvm] r38338 - in /hlvm/trunk/tools: SConscript hlvm-compiler/SConscript hlvm-config/hlvm-config.cpp hlvm-gentestcase/ hlvm-gentestcase/Generate.cpp hlvm-gentestcase/SConscript hlvm-gentestcase/hlvm-gentestcase.cpp

Reid Spencer reid at x10sys.com
Sat Jul 7 17:02:28 PDT 2007


Author: reid
Date: Sat Jul  7 19:02:28 2007
New Revision: 38338

URL: http://llvm.org/viewvc/llvm-project?rev=38338&view=rev
Log:
First cut at a tool for generating test cases. Right now all this does is
generate functions, function calls, variables, and constants. And, not very well
at that.

Added:
    hlvm/trunk/tools/hlvm-gentestcase/
    hlvm/trunk/tools/hlvm-gentestcase/Generate.cpp
    hlvm/trunk/tools/hlvm-gentestcase/SConscript
    hlvm/trunk/tools/hlvm-gentestcase/hlvm-gentestcase.cpp
Modified:
    hlvm/trunk/tools/SConscript
    hlvm/trunk/tools/hlvm-compiler/SConscript
    hlvm/trunk/tools/hlvm-config/hlvm-config.cpp

Modified: hlvm/trunk/tools/SConscript
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/tools/SConscript?rev=38338&r1=38337&r2=38338&view=diff

==============================================================================
--- hlvm/trunk/tools/SConscript (original)
+++ hlvm/trunk/tools/SConscript Sat Jul  7 19:02:28 2007
@@ -22,4 +22,10 @@
 #===----------------------------------------------------------------------===#
 from build import hlvm
 Import('env')
-hlvm.Dirs(env,['hlvm-config','hlvm-xml2xml','hlvm-compiler','hlvm'])
+hlvm.Dirs(env,[
+  'hlvm-config',
+  'hlvm-xml2xml',
+  'hlvm-compiler',
+  'hlvm-gentestcase',
+  'hlvm'
+])

Modified: hlvm/trunk/tools/hlvm-compiler/SConscript
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/tools/hlvm-compiler/SConscript?rev=38338&r1=38337&r2=38338&view=diff

==============================================================================
--- hlvm/trunk/tools/hlvm-compiler/SConscript (original)
+++ hlvm/trunk/tools/hlvm-compiler/SConscript Sat Jul  7 19:02:28 2007
@@ -35,6 +35,13 @@
    'LLVMLinker',
    'LLVMBCReader',
    'LLVMBCWriter',
+   'LLVMTransforms',
+   'LLVMipo',
+   'LLVMipa',
+   'LLVMScalarOpts',
+   'LLVMAnalysis',
+   'LLVMTarget',
+   'LLVMTransformUtils',
    'LLVMCore',
    'LLVMSupport',
    'LLVMbzip2',

Modified: hlvm/trunk/tools/hlvm-config/hlvm-config.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/tools/hlvm-config/hlvm-config.cpp?rev=38338&r1=38337&r2=38338&view=diff

==============================================================================
--- hlvm/trunk/tools/hlvm-config/hlvm-config.cpp (original)
+++ hlvm/trunk/tools/hlvm-config/hlvm-config.cpp Sat Jul  7 19:02:28 2007
@@ -91,7 +91,11 @@
   "ConstantInteger",
   "ConstantReal",
   "ConstantString",
-  "ConstantAggregate",
+  "ConstantPointer",
+  "ConstantArray",
+  "ConstantVector",
+  "ConstantStructure",
+  "ConstantContinuation",
   "ConstantExpression",
   "Variable",
   "Function",
@@ -227,6 +231,9 @@
   std::cout << "ConstantValues: " 
             << FirstConstantValueID << " -> "
             << LastConstantValueID << "\n";
+  std::cout << "ConstantAggregates: " 
+            << FirstConstantAggregateID << " -> "
+            << LastConstantAggregateID << "\n";
   std::cout << "Linkables: " 
             << FirstLinkableID << " -> "
             << LastLinkableID << "\n";

Added: hlvm/trunk/tools/hlvm-gentestcase/Generate.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/tools/hlvm-gentestcase/Generate.cpp?rev=38338&view=auto

==============================================================================
--- hlvm/trunk/tools/hlvm-gentestcase/Generate.cpp (added)
+++ hlvm/trunk/tools/hlvm-gentestcase/Generate.cpp Sat Jul  7 19:02:28 2007
@@ -0,0 +1,561 @@
+//===-- Generate Random Test Cases ------------------------------*- C++ -*-===//
+//
+//                      High Level Virtual Machine (HLVM)
+//
+// Copyright (C) 2006 Reid Spencer. All Rights Reserved.
+//
+// This software is free software; you can redistribute it and/or modify it 
+// under the terms of the GNU Lesser General Public License as published by 
+// the Free Software Foundation; either version 2.1 of the License, or (at 
+// your option) any later version.
+//
+// This software is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+// FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for 
+// more details.
+//
+// You should have received a copy of the GNU Lesser General Public License 
+// along with this library in the file named LICENSE.txt; if not, write to the 
+// Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
+// MA 02110-1301 USA
+//
+//===----------------------------------------------------------------------===//
+/// @file tools/hlvm-gentestcase/Generate.cpp
+/// @author Reid Spencer <reid at hlvm.org> (original author)
+/// @date 2006/05/04
+/// @since 0.1.0
+/// @brief Implements the test case generator for hlvm-gentestcase
+//===----------------------------------------------------------------------===//
+
+#include <hlvm/Base/Assert.h>
+#include <hlvm/AST/AST.h>
+#include <hlvm/AST/Constants.h>
+#include <hlvm/AST/Linkables.h>
+#include <hlvm/AST/Arithmetic.h>
+#include <hlvm/AST/BooleanOps.h>
+#include <hlvm/AST/ControlFlow.h>
+#include <hlvm/AST/MemoryOps.h>
+#include <hlvm/AST/InputOutput.h>
+#include <hlvm/AST/RealMath.h>
+#include <hlvm/AST/Bundle.h>
+#include <llvm/Support/CommandLine.h>
+#include <llvm/ADT/StringExtras.h>
+#include <stdlib.h>
+
+using namespace llvm;
+using namespace hlvm;
+
+namespace 
+{
+
+cl::opt<unsigned>
+  Complexity("complexity", cl::desc("Specify complexity of generated code"),
+      cl::value_desc("num"));
+
+cl::opt<unsigned>
+  Seed("seed", cl::desc("Specify random number generator seed"),
+      cl::value_desc("num"));
+
+cl::opt<unsigned>
+  Size("size",cl::desc("Specify size of generated code"),
+      cl::value_desc("num"));
+
+hlvm::AST* ast = 0;
+hlvm::URI* uri = 0;
+hlvm::Bundle* bundle = 0;
+hlvm::Program* program = 0;
+unsigned line = 0;
+typedef std::vector<hlvm::Value*> ValueList;
+typedef std::map<const hlvm::Type*,ValueList> TypeValueMap;
+TypeValueMap values;
+
+inline hlvm::Locator* 
+getLocator()
+{
+  return ast->new_Locator(uri,++line);
+}
+
+inline 
+int64_t randRange(int64_t low, int64_t high)
+{
+  return int64_t(random()) % (high-low) + low;
+}
+
+inline
+uint64_t randRange(uint64_t low, uint64_t high, bool discriminate)
+{
+  return uint64_t(random()) % (high-low) + low;
+}
+
+hlvm::Type*
+genType()
+{
+  Type* result = 0;
+  NodeIDs id = NodeIDs(randRange(FirstTypeID,LastTypeID));
+  switch (id) {
+    case BooleanTypeID:
+    case CharacterTypeID:
+    case OctetTypeID:
+    case UInt8TypeID:
+    case UInt16TypeID:
+    case UInt32TypeID:
+    case UInt64TypeID:
+    case UInt128TypeID:
+    case SInt8TypeID:
+    case SInt16TypeID:
+    case SInt32TypeID:
+    case SInt64TypeID:
+    case SInt128TypeID:
+    case Float32TypeID:
+    case Float44TypeID:
+    case Float64TypeID:
+    case Float80TypeID:
+    case Float128TypeID:
+    case AnyTypeID:
+    case StringTypeID:
+    case BufferTypeID:
+    case StreamTypeID:
+    case TextTypeID:
+      /* FALL THROUGH (unimplemented) */
+    case SignatureTypeID:
+      /* FALL THROUGH (unimplemented) */
+    case RationalTypeID:
+      /* FALL THROUGH (unimplemented) */
+    case IntegerTypeID:
+    {
+      Locator* loc = getLocator();
+      std::string name = "int_" + utostr(line);
+      result = ast->new_IntegerType(
+        name,randRange(1,64,true),randRange(0,1,true),loc);
+      break;
+    }
+    case RangeTypeID:
+    {
+      Locator* loc = getLocator();
+      std::string name = "range_" + utostr(line);
+      uint64_t limit = randRange(0,5000000000LL);
+      result = ast->new_RangeType(name,-int64_t(limit),int64_t(limit),loc);
+      break;
+    }
+    case EnumerationTypeID:
+    {
+      Locator* loc = getLocator();
+      std::string name = "enum_" + utostr(line);
+      EnumerationType* E = ast->new_EnumerationType(name,loc);
+      for (unsigned i = 0; i < Complexity; i++)
+        E->addEnumerator(name + "_" + utostr(i));
+      result = E;
+      break;
+    }
+    case RealTypeID:
+    {
+      Locator* loc = getLocator();
+      std::string name = "real_" + utostr(line);
+      result = ast->new_RealType(name,randRange(1,52),randRange(1,11),loc);
+      break;
+    }
+    case AliasTypeID:
+    {
+      Locator* loc = getLocator();
+      std::string name = "alias_" + utostr(line);
+      result = ast->new_AliasType(name,genType(),loc);
+      break;
+    }
+    case PointerTypeID:
+    {
+      Locator* loc = getLocator();
+      std::string name = "ptr_" + utostr(line);
+      result = ast->new_PointerType(name,genType(),loc);
+      break;
+    }
+    case ArrayTypeID:
+    {
+      Locator* loc = getLocator();
+      std::string name = "array_" + utostr(line);
+      result = ast->new_ArrayType(name,genType(),randRange(1,Complexity),loc);
+      break;
+    }
+    case VectorTypeID:
+    {
+      Locator* loc = getLocator();
+      std::string name = "vector_" + utostr(line);
+      result = ast->new_VectorType(name,genType(),randRange(1,Complexity),loc);
+      break;
+    }
+    case OpaqueTypeID:
+    case ContinuationTypeID:
+      /* FALL THROUGH (not implemented) */
+    case StructureTypeID:
+    {
+      Locator* loc = getLocator();
+      std::string name = "struct_" + utostr(line);
+      StructureType* S = ast->new_StructureType(name,loc);
+      for (unsigned i = 0; i < Complexity; ++i) {
+        FieldType* fld = 
+          ast->new_AliasType(name+"_"+utostr(i),genType(),getLocator());
+        fld->setParent(S);
+      }
+      result = S;
+      break;
+    }
+    default:
+      hlvmAssert(!"Invalid Type?");
+  }
+  hlvmAssert(result && "No type defined?");
+  result->setParent(bundle);
+  return result;
+}
+
+hlvm::Value*
+genValue(const hlvm::Type* Ty, bool is_constant = false)
+{
+  if (!is_constant && randRange(0,Complexity) < Complexity/2) {
+    // First look up an existing value in the map
+    TypeValueMap::iterator VI = values.find(Ty);
+    if (VI != values.end()) {
+      ValueList& VL = VI->second;
+      return VL[ randRange(0,VL.size()-1) ];
+    }
+  }
+
+  // Didn't find one in the map, so generate a variable or constant
+  ConstantValue* C = 0;
+  Locator* loc = getLocator();
+  NodeIDs id = Ty->getID();
+  switch (id) {
+    case BooleanTypeID:
+    {
+      C = ast->new_ConstantBoolean(
+          std::string("cbool_") + utostr(line), bool(randRange(0,1)), loc);
+      break;
+    }
+    case CharacterTypeID:
+    {
+      C = ast->new_ConstantCharacter(
+        std::string("cchar_") + utostr(line), "a", loc);
+      break;
+    }
+    case OctetTypeID:
+    {
+      C = ast->new_ConstantOctet(
+        std::string("coctet_") + utostr(line), 
+          static_cast<unsigned char>(randRange(0,255)), loc);
+      break;
+    }
+    case UInt8TypeID:
+    {
+      uint8_t val = uint8_t(randRange(0,255));
+      std::string val_str(utostr(val));
+      C = ast->new_ConstantInteger(
+        std::string("cu8_")+utostr(line),val_str,10,Ty,loc);
+      break;
+    }
+    case UInt16TypeID:
+    {
+      uint16_t val = uint16_t(randRange(0,65535));
+      std::string val_str(utostr(val));
+      C = ast->new_ConstantInteger(
+        std::string("cu16_")+utostr(line),val_str,10,Ty,loc);
+      break;
+    }
+    case UInt32TypeID:
+    {
+      uint32_t val = randRange(0,4000000000U);
+      std::string val_str(utostr(val));
+      C = ast->new_ConstantInteger(
+        std::string("cu32_")+utostr(line),val_str,10,Ty,loc);
+      break;
+    }
+    case UInt128TypeID:
+      /* FALL THROUGH (not implemented) */
+    case UInt64TypeID:
+    {
+      uint64_t val = randRange(0,4000000000U);
+      std::string val_str(utostr(val));
+      C = ast->new_ConstantInteger(
+        std::string("cu64_")+utostr(line),val_str,10,Ty,loc);
+      break;
+    }
+    case SInt8TypeID:
+    {
+      int8_t val = int8_t(randRange(-128,127));
+      std::string val_str(utostr(val));
+      C = ast->new_ConstantInteger(
+        std::string("cs8_")+utostr(line),val_str,10,Ty,loc);
+      break;
+    }
+    case SInt16TypeID:
+    {
+      int16_t val = int16_t(randRange(-32768,32767));
+      std::string val_str(utostr(val));
+      C = ast->new_ConstantInteger(
+        std::string("cs16_")+utostr(line),val_str,10,Ty,loc);
+      break;
+    }
+    case SInt32TypeID:
+    {
+      int32_t val = int32_t(randRange(-2000000000,2000000000));
+      std::string val_str(utostr(val));
+      C = ast->new_ConstantInteger(
+        std::string("cs32_")+utostr(line),val_str,10,Ty,loc);
+      break;
+    }
+    case SInt128TypeID:
+      /* FALL THROUGH (not implemented) */
+    case SInt64TypeID:
+    {
+      int64_t val = int64_t(randRange(-2000000000,2000000000));
+      std::string val_str(utostr(val));
+      C = ast->new_ConstantInteger(
+        std::string("cs64_")+utostr(line),val_str,10,Ty,loc);
+      break;
+    }
+    case Float32TypeID:
+    case Float44TypeID:
+    case Float64TypeID:
+    case Float80TypeID:
+    case Float128TypeID:
+    {
+      double val = double(randRange(-2000000000,2000000000));
+      std::string val_str(ftostr(val));
+      C = ast->new_ConstantReal(
+        std::string("cf32_")+utostr(line),val_str,Ty,loc);
+      break;
+    }
+    case AnyTypeID:
+    {
+      C = ast->new_ConstantAny(
+        std::string("cany_")+utostr(line),
+          cast<ConstantValue>(genValue(genType(),true)),loc);
+      break;
+    }
+    case StringTypeID:
+    {
+      std::string val;
+      for (unsigned i = 0 ; i < Complexity*Size; i++)
+        val += char(randRange(35,126));
+      C = ast->new_ConstantString(
+        std::string("cstr_")+utostr(line),val,loc);
+    }
+    case BufferTypeID:
+    case StreamTypeID:
+    case TextTypeID:
+      // hlvmAssert("Can't get constant for these types");
+      /* FALL THROUGH (unimplemented) */
+    case SignatureTypeID:
+      /* FALL THROUGH (unimplemented) */
+    case RationalTypeID:
+      /* FALL THROUGH (unimplemented) */
+    case IntegerTypeID:
+    {
+      Locator* loc = getLocator();
+      std::string name = "cint_" + utostr(line);
+      const IntegerType* IntTy = llvm::cast<IntegerType>(Ty);
+      int64_t max = 1 << (IntTy->getBits() < 63 ? IntTy->getBits() : 63);
+      int64_t val = randRange(int64_t(-max),int64_t(max-1));
+      std::string val_str( itostr(val) );
+      C = ast->new_ConstantInteger(name,val_str,10,Ty,loc);
+      break;
+    }
+    case RangeTypeID:
+    {
+      Locator* loc = getLocator();
+      std::string name = "crange_" + utostr(line);
+      const RangeType* RngTy = llvm::cast<RangeType>(Ty);
+      int64_t val = randRange(RngTy->getMin(),RngTy->getMax());
+      std::string val_str( itostr(val) );
+      C = ast->new_ConstantInteger(name,val_str,10,RngTy,loc);
+      break;
+    }
+    case EnumerationTypeID:
+    {
+      Locator* loc = getLocator();
+      std::string name = "cenum_" + utostr(line);
+      const EnumerationType* ETy = llvm::cast<EnumerationType>(Ty);
+      unsigned val = randRange(0,ETy->size()-1);
+      EnumerationType::const_iterator I = ETy->begin() + val;
+      C = ast->new_ConstantEnumerator(name,*I,ETy,loc);
+      break;
+    }
+    case RealTypeID:
+    {
+      double val = double(randRange(-2000000000,2000000000));
+      std::string val_str(ftostr(val));
+      C = ast->new_ConstantReal(
+        std::string("cf32_")+utostr(line),val_str,Ty,loc);
+      break;
+    }
+    case AliasTypeID:
+    {
+      C = cast<ConstantValue>(
+            genValue(llvm::cast<AliasType>(Ty)->getElementType(),true));
+      break;
+    }
+    case PointerTypeID:
+    {
+      const PointerType* PT = llvm::cast<PointerType>(Ty);
+      const Type* referent = PT->getElementType();
+      C = ast->new_ConstantPointer(
+          std::string("cptr_") + utostr(line),
+          cast<ConstantValue>(genValue(referent,true)),loc);
+      break;
+    }
+    case ArrayTypeID:
+    {
+      const ArrayType* AT = llvm::cast<ArrayType>(Ty);
+      const Type* elemTy = AT->getElementType();
+      unsigned nElems = randRange(1,AT->getMaxSize(),true);
+      std::vector<ConstantValue*> elems;
+      for (unsigned i = 0; i < nElems; i++)
+        elems.push_back(cast<ConstantValue>(genValue(elemTy,true)));
+      C = ast->new_ConstantArray(std::string("carray_") + utostr(line),
+          elems, AT, loc);
+      break;
+    }
+    case VectorTypeID:
+    {
+      const VectorType* VT = llvm::cast<VectorType>(Ty);
+      const Type* elemTy = VT->getElementType();
+      uint64_t nElems = VT->getSize();
+      std::vector<ConstantValue*> elems;
+      for (unsigned i = 0; i < nElems; i++)
+        elems.push_back(cast<ConstantValue>(genValue(elemTy,true)));
+      C = ast->new_ConstantVector(std::string("cvect_") + utostr(line),
+          elems, VT, loc);
+      break;
+    }
+    case OpaqueTypeID:
+      /* FALL THROUGH (not implemented) */
+    case ContinuationTypeID:
+      /* FALL THROUGH (not implemented) */
+    case StructureTypeID:
+    {
+      const StructureType* VT = llvm::cast<StructureType>(Ty);
+      std::vector<ConstantValue*> elems;
+      for (StructureType::const_iterator I = VT->begin(), E = VT->end(); 
+           I != E; ++I) {
+        Value* V = genValue((*I)->getElementType(),true);
+        elems.push_back(cast<ConstantValue>(V));
+      }
+      C = ast->new_ConstantStructure(std::string("cstruct_") + utostr(line),
+          elems, VT, loc);
+      break;
+    }
+    default:
+      hlvmAssert(!"Invalid Type?");
+  }
+
+  Value* result = 0;
+  if (is_constant || (randRange(0,Complexity*Size) < (Complexity*Size)/2))
+    result = C;
+  else {
+    Variable* var = ast->new_Variable(C->getName()+"_var",C->getType(),loc);
+    var->setIsConstant(false);
+    var->setInitializer(C);
+    result = var;
+  }
+
+  // Give the thing a place to live
+  result->setParent(bundle);
+
+  // Memoize the result
+  values[result->getType()].push_back(result);
+  return result;
+}
+
+CallOp*
+genCallTo(Function* F)
+{
+  std::vector<Operator*> args;
+  Operator* O = ast->new_ReferenceOp(F,getLocator());
+  args.push_back(O);
+  const SignatureType* sig = F->getSignature();
+  for (SignatureType::const_iterator I = sig->begin(), E = sig->end(); 
+       I != E; ++I)
+  {
+    Value* V = genValue((*I)->getElementType());
+    hlvmAssert(isa<ConstantValue>(V) || isa<Linkable>(V)) ;
+    Operator* O = ast->new_ReferenceOp(V,getLocator());
+    args.push_back(O);
+  }
+  return ast->new_MultiOp<CallOp>(args,getLocator());
+}
+
+hlvm::Block*
+genBlock()
+{
+  Block* B = ast->new_Block(getLocator());
+  return B;
+}
+
+hlvm::Function*
+genFunction(Type* resultType, unsigned numArgs)
+{
+  // Get the function name
+  Locator* loc = getLocator();
+  std::string name = "func_" + utostr(line);
+
+  // Get the signature
+  std::string sigName = name + "_type";
+  SignatureType* sig = ast->new_SignatureType(sigName,resultType,loc);
+  if (randRange(0,Complexity) <= int(Complexity/2))
+    sig->setIsVarArgs(true);
+  for (unsigned i = 0; i < numArgs; ++i )
+  {
+    std::string name = "arg_" + utostr(i+1);
+    AliasType* alias = ast->new_AliasType(name,genType(),loc);
+    alias->setParent(sig);
+  }
+
+  // Create the function and set its linkage
+  LinkageKinds LK = LinkageKinds(randRange(ExternalLinkage,InternalLinkage));
+  if (LK == AppendingLinkage)
+    LK = InternalLinkage;
+  Function* F = ast->new_Function(name,sig,loc);
+  F->setLinkageKind(LK);
+
+  // Create a block and set its parent
+  Block* B = genBlock();
+  B->setParent(F);
+
+  // Get the function result and return instruction
+  Operator* O = ast->new_ReferenceOp(genValue(F->getResultType()),getLocator());
+  ResultOp* rslt = ast->new_UnaryOp<ResultOp>(O,getLocator());
+  rslt->setParent(B);
+
+  ReturnOp* ret = ast->new_NilaryOp<ReturnOp>(getLocator());
+  ret->setParent(B);
+  
+  // Install the function in the value map
+  values[resultType].push_back(F);
+
+  return F;
+}
+
+}
+
+AST* 
+GenerateTestCase(const std::string& pubid, const std::string& bundleName)
+{
+  srandom(Seed);
+  ast = AST::create();
+  ast->setPublicID(pubid);
+  ast->setSystemID(bundleName);
+  uri = ast->new_URI(pubid);
+  bundle = ast->new_Bundle(bundleName,getLocator());
+  program = ast->new_Program(bundleName,getLocator());
+  program->setParent(bundle);
+  Block* blk = ast->new_Block(getLocator());
+  blk->setParent(program);
+  for (unsigned i = 0; i < Size; i++) {
+    Type* result = genType();
+    Type* argTy  = genType();
+    unsigned numArgs = int(randRange(0,int(Complexity/5)));
+    Function* F = genFunction(result,numArgs);
+    F->setParent(bundle);
+    CallOp* call = genCallTo(F);
+    call->setParent(blk);
+  }
+
+  return ast;
+}

Added: hlvm/trunk/tools/hlvm-gentestcase/SConscript
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/tools/hlvm-gentestcase/SConscript?rev=38338&view=auto

==============================================================================
--- hlvm/trunk/tools/hlvm-gentestcase/SConscript (added)
+++ hlvm/trunk/tools/hlvm-gentestcase/SConscript Sat Jul  7 19:02:28 2007
@@ -0,0 +1,40 @@
+#===-- Build Script For tools/hlvm-gentestcase ----------------*- Python -*-===#
+#
+#                      High Level Virtual Machine (HLVM)
+#
+# Copyright (C) 2006 Reid Spencer. All Rights Reserved.
+#
+# This software is free software; you can redistribute it and/or modify it 
+# under the terms of the GNU Lesser General Public License as published by 
+# the Free Software Foundation; either version 2.1 of the License, or (at 
+# your option) any later version.
+#
+# This software is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for 
+# more details.
+#
+# You should have received a copy of the GNU Lesser General Public License 
+# along with this library in the file named LICENSE.txt; if not, write to the 
+# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
+# MA 02110-1301 USA
+#
+#===----------------------------------------------------------------------===#
+from build import hlvm
+Import('env')
+prog = env.Program('hlvm-gentestcase', hlvm.GetAllCXXFiles(env),
+ LIBS=[
+   'HLVMWriter',
+   'HLVMPass',
+   'HLVMAST',
+   'HLVMBase',
+   'xml2',
+   'LLVMSupport',
+   'LLVMSystem',
+   'aprutil-1',
+   'apr-1',
+   'stdc++'
+  ]
+)
+hlvm.InstallProgram(env,prog)
+

Added: hlvm/trunk/tools/hlvm-gentestcase/hlvm-gentestcase.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/tools/hlvm-gentestcase/hlvm-gentestcase.cpp?rev=38338&view=auto

==============================================================================
--- hlvm/trunk/tools/hlvm-gentestcase/hlvm-gentestcase.cpp (added)
+++ hlvm/trunk/tools/hlvm-gentestcase/hlvm-gentestcase.cpp Sat Jul  7 19:02:28 2007
@@ -0,0 +1,104 @@
+//===-- hlvm-gentestcase main program ---------------------------*- C++ -*-===//
+//
+//                      High Level Virtual Machine (HLVM)
+//
+// Copyright (C) 2006 Reid Spencer. All Rights Reserved.
+//
+// This software is free software; you can redistribute it and/or modify it 
+// under the terms of the GNU Lesser General Public License as published by 
+// the Free Software Foundation; either version 2.1 of the License, or (at 
+// your option) any later version.
+//
+// This software is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+// FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for 
+// more details.
+//
+// You should have received a copy of the GNU Lesser General Public License 
+// along with this library in the file named LICENSE.txt; if not, write to the 
+// Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
+// MA 02110-1301 USA
+//
+//===----------------------------------------------------------------------===//
+/// @file tools/hlvm-gentestcase/hlvm-gentestcase.cpp
+/// @author Reid Spencer <reid at hlvm.org> (original author)
+/// @date 2006/05/04
+/// @since 0.1.0
+/// @brief Implements the main program for the hlvm-gentestcase executable
+//===----------------------------------------------------------------------===//
+
+#include <hlvm/Base/Memory.h>
+#include <hlvm/Writer/XMLWriter.h>
+#include <hlvm/Pass/Pass.h>
+#include <llvm/Support/CommandLine.h>
+#include <llvm/System/Signals.h>
+#include <fstream>
+#include <iostream>
+
+using namespace llvm;
+using namespace hlvm;
+static cl::opt<std::string>
+OutputFilename("o", cl::desc("Override output filename"),
+               cl::value_desc("filename"));
+
+static cl::opt<std::string>
+URL("url", cl::desc("Specify URL for compilation unit"),
+    cl::value_desc("url"));
+
+static cl::opt<std::string>
+BundleName("bundle", cl::desc("Specify bundle name"),
+    cl::value_desc("name"));
+
+extern AST* GenerateTestCase(const std::string& id, const std::string& bname);
+
+int main(int argc, char**argv) 
+{
+  try {
+    initialize(argc,argv);
+    cl::ParseCommandLineOptions(argc, argv, 
+      "hlvm-gentestcase - HLVM test case generator\n");
+
+    std::ostream *Out = &std::cout;  // Default to printing to stdout.
+
+    if (BundleName.empty())
+      BundleName = "generatedTestCase";
+
+    if (URL.empty())
+      URL = std::string("http://hlvm.org/src/test/generated/") + 
+              BundleName + ".hlx";
+
+    if (OutputFilename.empty())
+      OutputFilename = BundleName + ".hlx";
+
+    if (OutputFilename != "-") { // Not stdout?
+      Out = new std::ofstream(OutputFilename.c_str());
+      sys::RemoveFileOnSignal(sys::Path(OutputFilename));
+    }
+
+    if (!Out->good()) {
+      std::cerr << argv[0] << ": error opening " << OutputFilename
+                << ": sending to stdout instead!\n";
+      Out = &std::cout;
+    }
+
+    AST* tree = GenerateTestCase(URL,BundleName);
+    if (!validate(tree)) {
+      std::cerr << argv[0] << ": Generated test case did not validate.\n";
+      return 1;
+    }
+    XMLWriter* wrtr = XMLWriter::create(OutputFilename.c_str());
+    wrtr->write(tree);
+    delete wrtr;
+
+    if (Out != &std::cout) {
+      static_cast<std::ofstream*>(Out)->close();
+      delete Out;
+    }
+    return 0;
+  } catch (const std::string& msg) {
+    std::cerr << argv[0] << ": " << msg << "\n";
+  } catch (...) {
+    std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
+  }
+  return 1;
+}





More information about the llvm-commits mailing list