Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
doc/
/lib/
lljvm-*.jar
lljvm-demo-*.jar
lljvm-doc-*.zip
Expand Down
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ all:
cd java && $(MAKE) all
cd libc && $(MAKE) all
cd java && $(MAKE) dist
cp java/dist/lljvm.jar lljvm-${VERSION}.jar
mkdir -p lib
cp java/dist/lljvm.jar lib/lljvm-${VERSION}.jar
cp java/dist/lljvm-sources.jar lib/lljvm-sources-${VERSION}.jar
cp thirdparty/jasmin/jasmin.jar lib/


doc:
cd java && $(MAKE) doc
Expand Down Expand Up @@ -43,8 +47,8 @@ clean:
cd libc && $(MAKE) clean
cd demo && $(MAKE) clean
rm -rf doc
rm -f lljvm-${VERSION}.jar lljvm-demo-${VERSION}.jar \
lljvm-doc-${VERSION}.zip lljvm-backend
rm -f lljvm-backend lib/lljvm-${VERSION}.jar lib/lljvm-sources-${VERSION}.jar lib/jasmin.jar


distclean: clean
cd thirdparty && $(MAKE) distclean
34 changes: 25 additions & 9 deletions backend/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,23 @@ char JVMWriter::id = 0;
* @param td the target data for the platform
* @param o the output stream to be written to
* @param cls the binary name of the class to generate
* @param src the name of the source file, to use in the '.source'
* directive. If empty, one will be generated
* @param dbg the debugging level
* @param trc stream to output trace lines too. JVMWriter takes
* ownership of the pointer
*/
JVMWriter::JVMWriter(const TargetData *td, formatted_raw_ostream &o,
const std::string &cls, unsigned int dbg)
: FunctionPass(&id), targetData(td), out(o), classname(cls), debug(dbg) {}
const std::string &cls, const std::string &src,
unsigned int dbg, raw_fd_ostream *trc)
: FunctionPass(&id), targetData(td), out(o), classname(cls)
, sourcename(src), debug(dbg), trace(trc) {}


JVMWriter::~JVMWriter() {
if (trace)
delete trace;
}

/**
* Register required analysis information.
Expand Down Expand Up @@ -68,13 +80,16 @@ bool JVMWriter::runOnFunction(Function &f) {
bool JVMWriter::doInitialization(Module &m) {
module = &m;
instNum = 0;
trcLineNum = 0;

std::string modID = module->getModuleIdentifier();
size_t slashPos = modID.rfind('/');
if(slashPos == std::string::npos)
sourcename = modID;
else
sourcename = modID.substr(slashPos + 1);
if (sourcename.empty()) {
std::string modID = module->getModuleIdentifier();
size_t slashPos = modID.rfind('/');
if(slashPos == std::string::npos)
sourcename = modID;
else
sourcename = modID.substr(slashPos + 1);
}

if(!classname.empty()) {
for(std::string::iterator i = classname.begin(),
Expand All @@ -91,7 +106,6 @@ bool JVMWriter::doInitialization(Module &m) {
printFields();
printExternalMethods();
printConstructor();
printClInit();
printMainMethod();
return false;
}
Expand All @@ -103,5 +117,7 @@ bool JVMWriter::doInitialization(Module &m) {
* @return whether the module was modified (always false)
*/
bool JVMWriter::doFinalization(Module &m) {
if (trace)
trace->close();
return false;
}
23 changes: 20 additions & 3 deletions backend/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ using namespace llvm;
class JVMWriter : public FunctionPass {
/** The output stream */
formatted_raw_ostream &out;
/** Optional stream for generating trace file*/
raw_fd_ostream * const trace;
/** The name of the source file */
std::string sourcename;
/** The binary name of the generated class */
Expand All @@ -65,17 +67,26 @@ class JVMWriter : public FunctionPass {
DenseMap<const BasicBlock*, unsigned int> blockIDs;
/** Mapping of values to local variable numbers */
DenseMap<const Value*, unsigned int> localVars;
/** Mapping of unnamed values to number used to generate a temporary name */
DenseMap<const Value*, unsigned int> temporaryNames;
/** Number of registers allocated for the function */
unsigned int usedRegisters;
/** Local variable number of the pointer to the packed list of varargs */
unsigned int vaArgNum;
/** Current instruction number */
unsigned int instNum;
/** Current line num of trace file" */
unsigned int trcLineNum;

public:
//Note that JVMWriter takes ownership of trc and will
//delete it once it is destroyed
JVMWriter(const TargetData *td, formatted_raw_ostream &o,
const std::string &cls, unsigned int dbg);
const std::string &cls, const std::string &src,
unsigned int dbg, raw_fd_ostream *trc);

virtual ~JVMWriter();

private:
// backend.cpp
void getAnalysisUsage(AnalysisUsage &au) const;
Expand Down Expand Up @@ -154,7 +165,6 @@ class JVMWriter : public FunctionPass {
void printIndirectLoad(const Value *v);
void printIndirectLoad(const Type *ty);
void printIndirectStore(const Value *ptr, const Value *val);
void printIndirectStore(const Type *ty);

// name.cpp
std::string sanitizeName(std::string name);
Expand Down Expand Up @@ -186,12 +196,19 @@ class JVMWriter : public FunctionPass {
void printLabel(const char *label);
void printLabel(const std::string &label);

void printStartInvocationTag(int includeStackSize = 0);
void printEndInvocationTag(const std::string &sig, bool local=false);
void printGetField(const std::string &sig, bool local=false);
void printLoadClassNameForMethod(const std::string &sig);
void printDeclareLinkerFields();
void printInitLinkerFields();
void printTrc(const std::string &sig);

// sections.cpp
void printHeader();
void printFields();
void printExternalMethods();
void printConstructor();
void printClInit();
void printMainMethod();

// types.cpp
Expand Down
33 changes: 24 additions & 9 deletions backend/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,37 @@
*/
void JVMWriter::printBasicBlock(const BasicBlock *block) {
printLabel(getLabelName(block));
if (trace) {
if (block->hasName()) {
std::string n = block->getName();
printTrc(n + ":");
}
}
for(BasicBlock::const_iterator i = block->begin(), e = block->end();
i != e; i++) {
instNum++;
if(debug >= 3) {

if (trace)
printSimpleInstruction(".line", utostr(trcLineNum+1));
else if(debug >= 1)
printSimpleInstruction(".line", utostr(instNum));

if(debug >= 3 || trace) {
// print current instruction as comment
// note that this block of code significantly increases
// code generation time
std::string str;
raw_string_ostream ss(str); ss << *i;
std::string::size_type pos = 0;
while((pos = str.find("\n", pos)) != std::string::npos)
str.replace(pos++, 1, "\n;");
out << ';' << str << '\n';
ss.flush();
if (trace)
printTrc(str);
if (debug >= 3) {
std::string::size_type pos = 0;
while((pos = str.find("\n", pos)) != std::string::npos)
str.replace(pos++, 1, "\n;");
out << ';' << str << '\n';
}
}
if(debug >= 1)
printSimpleInstruction(".line", utostr(instNum));

if(i->getOpcode() == Instruction::PHI)
// don't handle phi instruction in current block
Expand All @@ -69,8 +84,8 @@ void JVMWriter::printInstruction(const Instruction *inst) {
if(inst->getNumOperands() >= 2) right = inst->getOperand(1);
switch(inst->getOpcode()) {
case Instruction::Ret:
printSimpleInstruction("invokestatic",
"lljvm/runtime/Memory/destroyStackFrame()V");
printStartInvocationTag();
printEndInvocationTag("lljvm/runtime/Memory/destroyStackFrame()V");
if(inst->getNumOperands() >= 1) {
printValueLoad(left);
printSimpleInstruction(
Expand Down
19 changes: 9 additions & 10 deletions backend/const.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,33 +170,32 @@ void JVMWriter::printConstLoad(const std::string &str, bool cstring) {
void JVMWriter::printStaticConstant(const Constant *c) {
if(isa<ConstantAggregateZero>(c) || c->isNullValue()) {
// zero initialised constant
printStartInvocationTag(1);
printPtrLoad(targetData->getTypeAllocSize(c->getType()));
printSimpleInstruction("invokestatic",
"lljvm/runtime/Memory/zero(II)I");
printEndInvocationTag("lljvm/runtime/Memory/zero(II)I");
return;
}
std::string typeDescriptor = getTypeDescriptor(c->getType());
switch(c->getType()->getTypeID()) {
case Type::IntegerTyID:
case Type::FloatTyID:
case Type::DoubleTyID:
printStartInvocationTag(1);
printConstLoad(c);
printSimpleInstruction("invokestatic",
"lljvm/runtime/Memory/pack(I" + typeDescriptor + ")I");
printEndInvocationTag("lljvm/runtime/Memory/pack(I" + typeDescriptor + ")I");
break;
case Type::ArrayTyID:
if(const ConstantArray *ca = dyn_cast<ConstantArray>(c))
if(ca->isString()) {
printStartInvocationTag(1);
bool cstring = ca->isCString();
printConstLoad(ca->getAsString(), cstring);
if(cstring)
printSimpleInstruction("invokestatic",
"lljvm/runtime/Memory/pack(ILjava/lang/String;)I");
printEndInvocationTag("lljvm/runtime/Memory/pack(ILjava/lang/String;)I");
else {
printSimpleInstruction("invokevirtual",
"java/lang/String/toCharArray()[C");
printSimpleInstruction("invokestatic",
"lljvm/runtime/Memory/pack(I[C)I");
printEndInvocationTag("lljvm/runtime/Memory/pack(I[C)I");
}
break;
}
Expand All @@ -207,6 +206,7 @@ void JVMWriter::printStaticConstant(const Constant *c) {
printStaticConstant(cast<Constant>(c->getOperand(i)));
break;
case Type::PointerTyID:
printStartInvocationTag(1);
if(const Function *f = dyn_cast<Function>(c))
printValueLoad(f);
else if(const GlobalVariable *g = dyn_cast<GlobalVariable>(c))
Expand All @@ -220,8 +220,7 @@ void JVMWriter::printStaticConstant(const Constant *c) {
errs() << "Constant = " << *c << '\n';
llvm_unreachable("Invalid static initializer");
}
printSimpleInstruction("invokestatic",
"lljvm/runtime/Memory/pack(I" + typeDescriptor + ")I");
printEndInvocationTag( "lljvm/runtime/Memory/pack(I" + typeDescriptor + ")I");
break;
default:
errs() << "TypeID = " << c->getType()->getTypeID() << '\n';
Expand Down
Loading