//===-- UpgradeParser.y - Upgrade parser for llvm assmbly -------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Reid Spencer and is distributed under the
// University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the bison parser for LLVM 1.9 assembly language.
//
//===----------------------------------------------------------------------===//
%{
#include "ParserInternals.h"
#include <llvm/ADT/StringExtras.h>
#include <algorithm>
#include <map>
#include <utility>
#include <iostream>
#include <cassert>
#define YYERROR_VERBOSE 1
#define YYINCLUDED_STDLIB_H
#define YYDEBUG 1
#define UPGRADE_SETCOND_OPS 0
int yylex(); // declaration" of xxx warnings.
int yyparse();
extern int yydebug;
static std::string CurFilename;
static std::ostream *O = 0;
std::istream* LexInput = 0;
unsigned SizeOfPointer = 32;
static uint64_t unique = 1;
typedef std::vector<TypeInfo> TypeVector;
static TypeVector EnumeratedTypes;
typedef std::map<std::string,TypeInfo> TypeMap;
static TypeMap NamedTypes;
static TypeMap Globals;
void destroy(ValueList* VL) {
while (!VL->empty()) {
ValueInfo& VI = VL->back();
VI.destroy();
VL->pop_back();
}
delete VL;
}
void UpgradeAssembly(const std::string &infile, std::istream& in,
std::ostream &out, bool debug)
{
Upgradelineno = 1;
CurFilename = infile;
LexInput = ∈
yydebug = debug;
O = &out;
if (yyparse()) {
std::cerr << "Parse failed.\n";
exit(1);
}
}
static void ResolveType(TypeInfo& Ty) {
if (Ty.oldTy == UnresolvedTy) {
TypeMap::iterator I = NamedTypes.find(*Ty.newTy);
if (I != NamedTypes.end()) {
Ty.oldTy = I->second.oldTy;
Ty.elemTy = I->second.elemTy;
} else {
std::string msg("Can't resolve type: ");
msg += *Ty.newTy;
yyerror(msg.c_str());
}
} else if (Ty.oldTy == NumericTy) {
unsigned ref = atoi(&((Ty.newTy->c_str())[1])); // Skip the '\\'
if (ref < EnumeratedTypes.size()) {
Ty.oldTy = EnumeratedTypes[ref].oldTy;
Ty.elemTy = EnumeratedTypes[ref].elemTy;
} else {
std::string msg("Can't resolve type: ");
msg += *Ty.newTy;
yyerror(msg.c_str());
}
}
// otherwise its already resolved.
}
static const char* getCastOpcode(
std::string& Source, const TypeInfo& SrcTy, const TypeInfo& DstTy)
{
unsigned SrcBits = SrcTy.getBitWidth();
unsigned DstBits = DstTy.getBitWidth();
const char* opcode = "bitcast";
// Run through the possibilities ...
if (DstTy.isIntegral()) { // Casting to integral
if (SrcTy.isIntegral()) { // Casting from integral
if (DstBits < SrcBits)
opcode = "trunc";
else if (DstBits > SrcBits) { // its an extension
if (SrcTy.isSigned())
opcode ="sext"; // signed -> SEXT
else
opcode = "zext"; // unsigned -> ZEXT
} else {
opcode = "bitcast"; // Same size, No-op cast
}
} else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
if (DstTy.isSigned())
opcode = "fptosi"; // FP -> sint
else
opcode = "fptoui"; // FP -> uint
} else if (SrcTy.isPacked()) {
assert(DstBits == SrcTy.getBitWidth() &&
"Casting packed to integer of different width");
opcode = "bitcast"; // same size, no-op cast
} else {
assert(SrcTy.isPointer() &&
"Casting from a value that is not first-class type");
opcode = "ptrtoint"; // ptr -> int
}
} else if (DstTy.isFloatingPoint()) { // Casting to floating pt
if (SrcTy.isIntegral()) { // Casting from integral
if (SrcTy.isSigned())
opcode = "sitofp"; // sint -> FP
else
opcode = "uitofp"; // uint -> FP
} else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
if (DstBits < SrcBits) {
opcode = "fptrunc"; // FP -> smaller FP
} else if (DstBits > SrcBits) {
opcode = "fpext"; // FP -> larger FP
} else {
opcode ="bitcast"; // same size, no-op cast
}
} else if (SrcTy.isPacked()) {
assert(DstBits == SrcTy.getBitWidth() &&
"Casting packed to floating point of different width");
opcode = "bitcast"; // same size, no-op cast
} else {
assert(0 && "Casting pointer or non-first class to float");
}
} else if (DstTy.isPacked()) {
if (SrcTy.isPacked()) {
assert(DstTy.getBitWidth() == SrcTy.getBitWidth() &&
"Casting packed to packed of different widths");
opcode = "bitcast"; // packed -> packed
} else if (DstTy.getBitWidth() == SrcBits) {
opcode = "bitcast"; // float/int -> packed
} else {
assert(!"Illegal cast to packed (wrong type or size)");
}
} else if (DstTy.isPointer()) {
if (SrcTy.isPointer()) {
opcode = "bitcast"; // ptr -> ptr
} else if (SrcTy.isIntegral()) {
opcode = "inttoptr"; // int -> ptr
} else {
assert(!"Casting invalid type to pointer");
}
} else {
assert(!"Casting to type that is not first-class");
}
return opcode;
}
static std::string getCastUpgrade(
const std::string& Src, TypeInfo& SrcTy, TypeInfo& DstTy, bool isConst)
{
std::string Result;
std::string Source = Src;
if (SrcTy.isFloatingPoint() && DstTy.isPointer()) {
// fp -> ptr cast is no longer supported but we must upgrade this
// by doing a double cast: fp -> int -> ptr
if (isConst)
Source = "ulong fptoui(" + Source + " to ulong)";
else {
*O << " %cast_upgrade" << unique << " = fptoui " << Source
<< " to ulong\n";
Source = "ulong %cast_upgrade" + llvm::utostr(unique);
}
// Update the SrcTy for the getCastOpcode call below
SrcTy.destroy();
SrcTy.newTy = new std::string("ulong");
SrcTy.oldTy = ULongTy;
} else if (DstTy.oldTy == BoolTy) {
// cast ptr %x to bool was previously defined as setne ptr %x, null
// The ptrtoint semantic