Example #1
0
 CG::ExprValue ConstFloat::buildExprValue( CG::BasicBlockBuilder &basicBlockBuilder, CG::Usage usage, std::string const &lValueErrorDesc ) const
 {
   if ( usage == CG::USAGE_LVALUE )
     throw Exception( "constants cannot be used as l-values" );
   RC::ConstHandle<CG::FloatAdapter> floatAdapter = basicBlockBuilder.getManager()->getFloat64Adapter();
   floatAdapter->llvmCompileToModule( basicBlockBuilder.getModuleBuilder() );
   double value = Util::parseDouble( m_valueString );
   return CG::ExprValue( floatAdapter, CG::USAGE_RVALUE, basicBlockBuilder.getContext(), floatAdapter->llvmConst( basicBlockBuilder.getContext(), value ) );
 }
Example #2
0
 CG::ExprValue ConstString::buildExprValue( CG::BasicBlockBuilder &basicBlockBuilder, CG::Usage usage, std::string const &lValueErrorDesc ) const
 {
   if ( usage == CG::USAGE_LVALUE )
     throw Exception( "constants cannot be used as l-values" );
   std::string unquotedValue;
   try
   {
     unquotedValue = Util::parseQuotedString( m_value );
   }
   catch ( Exception e )
   {
     throw CG::Error( getLocation(), e.getDesc() + "(" + m_value + ")" );
   }
   RC::ConstHandle<CG::ConstStringAdapter> constStringAdapter = basicBlockBuilder.getManager()->getConstStringAdapter();
   constStringAdapter->llvmCompileToModule( basicBlockBuilder.getModuleBuilder() );
   RC::ConstHandle<CG::StringAdapter> stringAdapter = basicBlockBuilder.getManager()->getStringAdapter();
   stringAdapter->llvmCompileToModule( basicBlockBuilder.getModuleBuilder() );
   return CG::ExprValue( constStringAdapter, CG::USAGE_RVALUE, basicBlockBuilder.getContext(), constStringAdapter->llvmConst( basicBlockBuilder, unquotedValue.data(), unquotedValue.length() ) ).castTo( basicBlockBuilder, stringAdapter );
 }
 CG::ExprValue CreateConstValue::buildExprValue( CG::BasicBlockBuilder &basicBlockBuilder, CG::Usage usage, std::string const &lValueErrorDesc ) const
 {
   if ( usage == CG::USAGE_LVALUE )
     throw Exception( "cannot be used as l-values" );
   
   CG::ExprType childExprType = m_child->getExprType( basicBlockBuilder );
   RC::ConstHandle<CG::ValueProducerAdapter> valueProducerAdapter = basicBlockBuilder.getManager()->getValueProducerOf( childExprType.getAdapter() );
   
   RC::Handle<CG::Context> context = basicBlockBuilder.getContext();
   llvm::LLVMContext &llvmContext = context->getLLVMContext();
   
   std::vector<llvm::Type const *> argTypes;
   argTypes.push_back( llvm::Type::getInt8PtrTy( llvmContext ) );
   argTypes.push_back( llvm::Type::getInt8PtrTy( llvmContext ) );
   argTypes.push_back( valueProducerAdapter->llvmLType( context ) );
   llvm::FunctionType const *funcType = llvm::FunctionType::get( llvm::Type::getVoidTy( llvmContext ), argTypes, false );
   llvm::Constant *func = basicBlockBuilder.getModuleBuilder()->getOrInsertFunction( "__MR_CreateConstValue", funcType );
   
   CG::ExprValue childExprRValue = m_child->buildExprValue( basicBlockBuilder, CG::USAGE_RVALUE, lValueErrorDesc );
   llvm::Value *childExprLValue = childExprType.getAdapter()->llvmRValueToLValue( basicBlockBuilder, childExprRValue.getValue() );
   llvm::Value *resultLValue = valueProducerAdapter->llvmAlloca( basicBlockBuilder, "result" );
   valueProducerAdapter->llvmInit( basicBlockBuilder, resultLValue );
   basicBlockBuilder.getScope().put(
     CG::VariableSymbol::Create(
       CG::ExprValue(
         valueProducerAdapter,
         CG::USAGE_LVALUE,
         context,
         resultLValue
         )
       )
     );
   
   basicBlockBuilder->CreateCall3(
     func,
     valueProducerAdapter->llvmAdapterPtr( basicBlockBuilder ),
     basicBlockBuilder->CreatePointerCast(
       childExprLValue,
       llvm::Type::getInt8PtrTy( llvmContext )
       ),
     resultLValue
     );
   
   return CG::ExprValue(
     valueProducerAdapter,
     CG::USAGE_RVALUE,
     context,
     valueProducerAdapter->llvmLValueToRValue( basicBlockBuilder, resultLValue )
     );
 }
Example #4
0
 CG::ExprType CreateArrayMap::getExprType( CG::BasicBlockBuilder &basicBlockBuilder ) const
 {
   RC::ConstHandle<CG::Symbol> operatorSymbol = basicBlockBuilder.getScope().get( m_operatorName );
   if ( !operatorSymbol )
     throw CG::Error( getLocation(), _(m_operatorName) + ": operator not found" );
   if ( !operatorSymbol->isPencil() )
     throw CG::Error( getLocation(), _(m_operatorName) + ": not an operator" );
   RC::ConstHandle<CG::PencilSymbol> pencil = RC::ConstHandle<CG::PencilSymbol>::StaticCast( operatorSymbol );
   CG::Function const *function = pencil->getUniqueFunction( getLocation(), "operator " + _(m_operatorName) );
   std::vector<CG::FunctionParam> const &operatorParams = function->getParams();
   RC::ConstHandle<CG::Adapter> outputAdapter = operatorParams[1].getAdapter();
   RC::ConstHandle<CG::ArrayProducerAdapter> outputArrayProducerAdapter = basicBlockBuilder.getManager()->getArrayProducerOf( outputAdapter );
   return CG::ExprType( outputArrayProducerAdapter, CG::USAGE_RVALUE );
 }
Example #5
0
    void Report::llvmCompileToBuilder( CG::BasicBlockBuilder &basicBlockBuilder, CG::Diagnostics &diagnostics ) const
    {
      RC::ConstHandle< CG::StringAdapter > stringAdapter = basicBlockBuilder.getManager()->getStringAdapter();
      stringAdapter->llvmCompileToModule( basicBlockBuilder.getModuleBuilder() );

      try
      {
        CG::Scope subScope( basicBlockBuilder.getScope() );
        CG::BasicBlockBuilder subBBB( basicBlockBuilder, subScope );
        CG::ExprValue exprExprValue = m_expr->buildExprValue( subBBB, CG::USAGE_RVALUE, "cannot be an l-value" );
        llvm::Value *stringRValue = stringAdapter->llvmCast( subBBB, exprExprValue );
        stringAdapter->llvmReport( subBBB, stringRValue );
        subScope.llvmUnwind( subBBB );
      }
      catch ( CG::Error e )
      {
        addError( diagnostics, e );
      }
      catch ( Exception e )
      {
        addError( diagnostics, e );
      }
    }
Example #6
0
 CG::ExprType ConstString::getExprType( CG::BasicBlockBuilder &basicBlockBuilder ) const
 {
   RC::ConstHandle<CG::Adapter> adapter = basicBlockBuilder.getManager()->getStringAdapter();
   adapter->llvmCompileToModule( basicBlockBuilder.getModuleBuilder() );
   return CG::ExprType( adapter, CG::USAGE_RVALUE );
 }
 CG::ExprType CreateConstValue::getExprType( CG::BasicBlockBuilder &basicBlockBuilder ) const
 {
   return CG::ExprType( basicBlockBuilder.getManager()->getValueProducerOf( m_child->getExprType( basicBlockBuilder ).getAdapter() ), CG::USAGE_RVALUE );
 }
    CG::ExprValue CreateArrayGenerator::buildExprValue( CG::BasicBlockBuilder &basicBlockBuilder, CG::Usage usage, std::string const &lValueErrorDesc ) const
    {
      if ( usage == CG::USAGE_LVALUE )
        throw Exception( "cannot be used as l-values" );
      
      RC::Handle<CG::Context> context = basicBlockBuilder.getContext();
      llvm::LLVMContext &llvmContext = context->getLLVMContext();
      RC::ConstHandle<CG::SizeAdapter> sizeAdapter = basicBlockBuilder.getManager()->getSizeAdapter();
            
      RC::ConstHandle<CG::Symbol> operatorSymbol = basicBlockBuilder.getScope().get( m_operatorName );
      if ( !operatorSymbol )
        throw CG::Error( getLocation(), _(m_operatorName) + ": operator not found" );
      if ( !operatorSymbol->isPencil() )
        throw CG::Error( getLocation(), _(m_operatorName) + ": not an operator" );
      RC::ConstHandle<CG::PencilSymbol> pencil = RC::ConstHandle<CG::PencilSymbol>::StaticCast( operatorSymbol );
      CG::Function const *function = pencil->getUniqueFunction( getLocation(), "operator " + _(m_operatorName) );
      std::vector<CG::FunctionParam> const &operatorParams = function->getParams();
      if ( operatorParams.size() < 1 )
        throw MR::ArrayGeneratorOperator::GetPrototypeException();

      CG::ExprType countExprType = m_count->getExprType( basicBlockBuilder );
      if ( !RT::isValueProducer( countExprType.getAdapter()->getType() ) )
        throw CG::Error( getLocation(), "count must be a value producer" );
      RC::ConstHandle<CG::ValueProducerAdapter> countValueProducerAdapter = RC::ConstHandle<CG::ValueProducerAdapter>::StaticCast( countExprType.getAdapter() );
      RC::ConstHandle<CG::Adapter> countAdapter = countValueProducerAdapter->getValueAdapter();
      if ( countAdapter != sizeAdapter )
        throw CG::Error( getLocation(), "count value type must be 'Size'" );
      CG::ExprValue countValueProducerExprValue = m_count->buildExprValue( basicBlockBuilder, CG::USAGE_RVALUE, lValueErrorDesc );
        
      if ( operatorParams[0].getUsage() != CG::USAGE_LVALUE )
        throw CG::Error( getLocation(), "operator value parameter must be an 'io' parameter" );
      RC::ConstHandle<CG::Adapter> outputAdapter = operatorParams[0].getAdapter();
      RC::ConstHandle<CG::ArrayProducerAdapter> outputArrayProducerAdapter = basicBlockBuilder.getManager()->getArrayProducerOf( outputAdapter );
      llvm::Value *resultLValue = outputArrayProducerAdapter->llvmAlloca( basicBlockBuilder, "result" );
      outputArrayProducerAdapter->llvmInit( basicBlockBuilder, resultLValue );
      basicBlockBuilder.getScope().put(
        CG::VariableSymbol::Create(
          CG::ExprValue(
            outputArrayProducerAdapter,
            CG::USAGE_LVALUE,
            context,
            resultLValue
            )
          )
        );
    
      bool needCall = true;
      if ( operatorParams.size() >= 2 )
      {
        if ( operatorParams[1].getAdapter() != sizeAdapter )
          throw CG::Error( getLocation(), "operator index parameter type (" + operatorParams[1].getAdapter()->getUserName() + ") must be 'Size'" );
        if ( operatorParams[1].getUsage() != CG::USAGE_RVALUE )
          throw CG::Error( getLocation(), "operator index parameter must be an 'in' parameter" );
          
        if ( operatorParams.size() >= 3 )
        {
          if ( operatorParams[2].getAdapter() != sizeAdapter )
            throw CG::Error( getLocation(), "operator index parameter type (" + operatorParams[2].getAdapter()->getUserName() + ") must be 'Size'" );
          if ( operatorParams[2].getUsage() != CG::USAGE_RVALUE )
            throw CG::Error( getLocation(), "operator index parameter must be an 'in' parameter" );
            
          if ( operatorParams.size() >= 4 )
          {
            if ( operatorParams.size() > 4 )
              throw MR::ArrayGeneratorOperator::GetPrototypeException();
              
            if ( !m_shared )
              throw CG::Error( getLocation(), "operator takes a shared value but no shared value is provided" );
              
            CG::ExprType sharedExprType = m_shared->getExprType( basicBlockBuilder );
            if ( !RT::isValueProducer( sharedExprType.getAdapter()->getType() ) )
              throw CG::Error( getLocation(), "shared value must be a value producer" );
            RC::ConstHandle<CG::ValueProducerAdapter> sharedValueProducerAdapter = RC::ConstHandle<CG::ValueProducerAdapter>::StaticCast( sharedExprType.getAdapter() );
            RC::ConstHandle<CG::Adapter> sharedAdapter = sharedValueProducerAdapter->getValueAdapter();

            if ( operatorParams[3].getAdapter() != sharedAdapter )
              throw CG::Error( getLocation(), "operator shared value parameter type (" + operatorParams[3].getAdapter()->getUserName() + ") does not match shared value type (" + sharedAdapter->getUserName() + ")" );
            if ( operatorParams[3].getUsage() != CG::USAGE_RVALUE )
              throw CG::Error( getLocation(), "operator shared value parameter must be an 'in' parameter" );

            CG::ExprValue sharedExprRValue = m_shared->buildExprValue( basicBlockBuilder, CG::USAGE_RVALUE, lValueErrorDesc );

            std::vector<llvm::Type const *> argTypes;
            argTypes.push_back( llvm::Type::getInt8PtrTy( llvmContext ) ); // function
            argTypes.push_back( sizeAdapter->llvmRType( context ) ); // numParams
            argTypes.push_back( countValueProducerAdapter->llvmLType( context ) ); // count value producer
            argTypes.push_back( sharedValueProducerAdapter->llvmLType( context ) ); // shared value producer
            argTypes.push_back( llvm::Type::getInt8PtrTy( llvmContext ) ); // output array producer adapter
            argTypes.push_back( outputArrayProducerAdapter->llvmLType( context ) ); // output array producer
            llvm::FunctionType const *funcType = llvm::FunctionType::get( llvm::Type::getVoidTy( llvmContext ), argTypes, false );
            llvm::Constant *func = basicBlockBuilder.getModuleBuilder()->getOrInsertFunction( "__MR_CreateArrayGenerator_4", funcType );
            
            std::vector<llvm::Value *> args;
            args.push_back( basicBlockBuilder->CreateBitCast(
              function->getLLVMFunction(),
              llvm::Type::getInt8PtrTy( llvmContext )
              ) );
            args.push_back( sizeAdapter->llvmConst( context, operatorParams.size() ) );
            args.push_back( countValueProducerExprValue.getValue() );
            args.push_back( sharedExprRValue.getValue() );
            args.push_back( outputArrayProducerAdapter->llvmAdapterPtr( basicBlockBuilder ) );
            args.push_back( resultLValue );
            basicBlockBuilder->CreateCall( func, args.begin(), args.end() );
            
            needCall = false;
          }
        }
      }
      
      if ( needCall )
      {
        std::vector<llvm::Type const *> argTypes;
        argTypes.push_back( llvm::Type::getInt8PtrTy( llvmContext ) ); // function
        argTypes.push_back( sizeAdapter->llvmRType( context ) ); // numParams
        argTypes.push_back( countValueProducerAdapter->llvmLType( context ) ); // count value producer
        argTypes.push_back( llvm::Type::getInt8PtrTy( llvmContext ) ); // output array producer adapter
        argTypes.push_back( outputArrayProducerAdapter->llvmLType( context ) ); // output array producer
        llvm::FunctionType const *funcType = llvm::FunctionType::get( llvm::Type::getVoidTy( llvmContext ), argTypes, false );
        llvm::Constant *func = basicBlockBuilder.getModuleBuilder()->getOrInsertFunction( "__MR_CreateArrayGenerator_3", funcType );
        
        std::vector<llvm::Value *> args;
        args.push_back( basicBlockBuilder->CreateBitCast(
          function->getLLVMFunction(),
          llvm::Type::getInt8PtrTy( llvmContext )
          ) );
        args.push_back( sizeAdapter->llvmConst( context, operatorParams.size() ) );
        args.push_back( countValueProducerExprValue.getValue() );
        args.push_back( outputArrayProducerAdapter->llvmAdapterPtr( basicBlockBuilder ) );
        args.push_back( resultLValue );
        basicBlockBuilder->CreateCall( func, args.begin(), args.end() );
      }

      return CG::ExprValue(
        outputArrayProducerAdapter,
        CG::USAGE_RVALUE,
        context,
        outputArrayProducerAdapter->llvmLValueToRValue( basicBlockBuilder, resultLValue )
        );
    }
Example #9
0
 CG::ExprValue IndexOp::buildExprValue( CG::BasicBlockBuilder &basicBlockBuilder, CG::Usage usage, std::string const &lValueErrorDesc ) const
 {
   CG::ExprValue result( basicBlockBuilder.getContext() );
   try
   {
     CG::ExprValue arrayExprValue = m_expr->buildExprValue( basicBlockBuilder, usage, lValueErrorDesc );
     RC::ConstHandle<CG::Adapter> adapter = arrayExprValue.getAdapter();
     
     if ( RT::isArray( arrayExprValue.getAdapter()->getType() ) )
     {
       RC::ConstHandle<CG::ArrayAdapter> arrayAdapter = RC::ConstHandle<CG::ArrayAdapter>::StaticCast( adapter );
       
       CG::ExprValue indexExprValue = m_indexExpr->buildExprValue( basicBlockBuilder, CG::USAGE_RVALUE, lValueErrorDesc );
       RC::ConstHandle< CG::SizeAdapter > sizeAdapter = basicBlockBuilder.getManager()->getSizeAdapter();
       llvm::Value *indexExprRValue = sizeAdapter->llvmCast( basicBlockBuilder, indexExprValue );
       if ( indexExprRValue )
       {
         switch ( usage )
         {
           case CG::USAGE_LVALUE:
             result = CG::ExprValue(
               arrayAdapter->getMemberAdapter(),
               CG::USAGE_LVALUE,
               basicBlockBuilder.getContext(),
               arrayAdapter->llvmNonConstIndexOp(
                 basicBlockBuilder,
                 arrayExprValue.getValue(),
                 indexExprRValue,
                 &getLocation()
                 )
               );
             break;
           default:
             result = CG::ExprValue(
               arrayAdapter->getMemberAdapter(),
               CG::USAGE_RVALUE,
               basicBlockBuilder.getContext(),
               arrayAdapter->llvmConstIndexOp(
                 basicBlockBuilder,
                 arrayExprValue.getValue(),
                 indexExprRValue,
                 &getLocation()
                 )
               );
             break;
         }
       }
     }
     else if ( RT::isDict( arrayExprValue.getAdapter()->getType() ) )
     {
       RC::ConstHandle<CG::DictAdapter> dictAdapter = RC::ConstHandle<CG::DictAdapter>::StaticCast( adapter );
       
       CG::ExprValue indexExprValue = m_indexExpr->buildExprValue( basicBlockBuilder, CG::USAGE_RVALUE, lValueErrorDesc );
       RC::ConstHandle<CG::ComparableAdapter> keyAdapter = dictAdapter->getKeyAdapter();
       llvm::Value *indexExprRValue = keyAdapter->llvmCast( basicBlockBuilder, indexExprValue );
       if ( indexExprRValue )
       {
         switch ( usage )
         {
           case CG::USAGE_LVALUE:
             result = CG::ExprValue(
               dictAdapter->getValueAdapter(),
               CG::USAGE_LVALUE,
               basicBlockBuilder.getContext(),
               dictAdapter->llvmGetLValue(
                 basicBlockBuilder,
                 arrayExprValue.getValue(),
                 indexExprRValue
                 )
               );
             break;
           default:
             result = CG::ExprValue(
               dictAdapter->getValueAdapter(),
               CG::USAGE_RVALUE,
               basicBlockBuilder.getContext(),
               dictAdapter->llvmGetRValue(
                 basicBlockBuilder,
                 arrayExprValue.getValue(),
                 indexExprRValue
                 )
               );
             break;
         }
       }
     }
     else throw CG::Error( getLocation(), "only arrays and dictionaries can be indexed" );
   }
   catch ( CG::Error e )
   {
     throw e;
   }
   catch ( Exception e )
   {
     throw CG::Error( getLocation(), e );
   }
   return result;
 }