// Emit top-level description of module, including inline asm,
// descriptors for global variables, and function prototype info.
static void WriteModuleInfo(const Module *M, const NaClValueEnumerator &VE,
                            NaClBitstreamWriter &Stream) {
  DEBUG(dbgs() << "-> WriteModuleInfo\n");

  // Emit the function proto information. Note: We do this before
  // global variables, so that global variable initializations can
  // refer to the functions without a forward reference.
  SmallVector<unsigned, 64> Vals;
  for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
    // FUNCTION:  [type, callingconv, isproto, linkage]
    Type *Ty = F->getType()->getPointerElementType();
    Vals.push_back(VE.getTypeID(Ty));
    Vals.push_back(GetEncodedCallingConv(F->getCallingConv()));
    Vals.push_back(F->isDeclaration());
    Vals.push_back(getEncodedLinkage(F));

    unsigned AbbrevToUse = 0;
    Stream.EmitRecord(naclbitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
    Vals.clear();
  }

  // Emit the global variable information.
  WriteGlobalVars(M, VE, Stream);
  DEBUG(dbgs() << "<- WriteModuleInfo\n");
}
bool PNaClABIVerifyModule::runOnModule(Module &M) {
  if (!M.getModuleInlineAsm().empty()) {
    Reporter->addError() <<
        "Module contains disallowed top-level inline assembly\n";
  }

  for (Module::const_global_iterator MI = M.global_begin(), ME = M.global_end();
       MI != ME; ++MI) {
    checkGlobalIsFlattened(MI);
    checkGlobalValueCommon(MI);

    if (MI->isThreadLocal()) {
      Reporter->addError() << "Variable " << MI->getName() <<
          " has disallowed \"thread_local\" attribute\n";
    }
  }

  // No aliases allowed for now.
  for (Module::alias_iterator MI = M.alias_begin(),
           E = M.alias_end(); MI != E; ++MI) {
    Reporter->addError() << "Variable " << MI->getName() <<
        " is an alias (disallowed)\n";
  }

  for (Module::const_iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) {
    if (MI->isIntrinsic()) {
      // Check intrinsics.
      if (!isWhitelistedIntrinsic(MI, MI->getIntrinsicID())) {
        Reporter->addError() << "Function " << MI->getName()
                             << " is a disallowed LLVM intrinsic\n";
      }
    } else {
      // Check types of functions and their arguments.  Not necessary
      // for intrinsics, whose types are fixed anyway, and which have
      // argument types that we disallow such as i8.
      if (!PNaClABITypeChecker::isValidFunctionType(MI->getFunctionType())) {
        Reporter->addError() << "Function " << MI->getName()
            << " has disallowed type: "
            << PNaClABITypeChecker::getTypeName(MI->getFunctionType())
            << "\n";
      }
      // This check is disabled in streaming mode because it would
      // reject a function that is defined but not read in yet.
      // Unfortunately this means we simply don't check this property
      // when translating a pexe in the browser.
      // TODO(mseaborn): Enforce this property in the bitcode reader.
      if (!StreamingMode && MI->isDeclaration()) {
        Reporter->addError() << "Function " << MI->getName()
                             << " is declared but not defined (disallowed)\n";
      }
      if (!MI->getAttributes().isEmpty()) {
        Reporter->addError()
            << "Function " << MI->getName() << " has disallowed attributes:"
            << getAttributesAsString(MI->getAttributes()) << "\n";
      }
      if (MI->getCallingConv() != CallingConv::C) {
        Reporter->addError()
            << "Function " << MI->getName()
            << " has disallowed calling convention: "
            << MI->getCallingConv() << "\n";
      }
    }

    checkGlobalValueCommon(MI);

    if (MI->hasGC()) {
      Reporter->addError() << "Function " << MI->getName() <<
          " has disallowed \"gc\" attribute\n";
    }
    // Knowledge of what function alignments are useful is
    // architecture-specific and sandbox-specific, so PNaCl pexes
    // should not be able to specify function alignment.
    if (MI->getAlignment() != 0) {
      Reporter->addError() << "Function " << MI->getName() <<
          " has disallowed \"align\" attribute\n";
    }
  }

  // Check named metadata nodes
  for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
           E = M.named_metadata_end(); I != E; ++I) {
    if (!isWhitelistedMetadata(I)) {
      Reporter->addError() << "Named metadata node " << I->getName()
                           << " is disallowed\n";
    }
  }

  Reporter->checkForFatalErrors();
  return false;
}