[vmkit-commits] [vmkit] r198687 - Full implementation of tableswitch.

Gael Thomas gael.thomas at lip6.fr
Tue Jan 7 04:29:13 PST 2014


Author: gthomas
Date: Tue Jan  7 06:29:12 2014
New Revision: 198687

URL: http://llvm.org/viewvc/llvm-project?rev=198687&view=rev
Log:
Full implementation of tableswitch.

Modified:
    vmkit/branches/mcjit/include/j3/j3codegen.h
    vmkit/branches/mcjit/lib/j3/vm/j3codegen.cc

Modified: vmkit/branches/mcjit/include/j3/j3codegen.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/include/j3/j3codegen.h?rev=198687&r1=198686&r2=198687&view=diff
==============================================================================
--- vmkit/branches/mcjit/include/j3/j3codegen.h (original)
+++ vmkit/branches/mcjit/include/j3/j3codegen.h Tue Jan  7 06:29:12 2014
@@ -146,6 +146,7 @@ namespace j3 {
 		void                newObject(J3Class* cl);
 
 		void                lookupSwitch();
+		void                tableSwitch();
 
 		void                ldc(uint32_t idx);
 

Modified: vmkit/branches/mcjit/lib/j3/vm/j3codegen.cc
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/lib/j3/vm/j3codegen.cc?rev=198687&r1=198686&r2=198687&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/j3/vm/j3codegen.cc (original)
+++ vmkit/branches/mcjit/lib/j3/vm/j3codegen.cc Tue Jan  7 06:29:12 2014
@@ -41,10 +41,10 @@ J3CodeGen::J3CodeGen(vmkit::BumpAllocato
 
 #if 0
 	/* usefull to debug a single function */
-	if(   cl->name() == vm->names()->get("sun/misc/Launcher") &&
-				method->name() == vm->names()->get("getFileURL") &&
-				method->signature()->name() == vm->names()->get("(Ljava/io/File;)Ljava/net/URL;") ) {
-		vm->options()->debugTranslate = 2;
+	if(   cl->name() == vm->names()->get("java/lang/CharacterData") &&
+				method->name() == vm->names()->get("of") &&
+				method->signature()->name() == vm->names()->get("(I)Ljava/lang/CharacterData;") ) {
+		vm->options()->debugTranslate = 3;
 	}
 #endif
 
@@ -704,7 +704,7 @@ void J3CodeGen::ldc(uint32_t idx) {
 void J3CodeGen::lookupSwitch() {
 	codeReader->seek(((codeReader->tell() - 1) & -4) + 4, J3Reader::SeekSet);
 	llvm::Value* val = stack.pop();
-	llvm::BasicBlock* def = forwardBranch("lookupswitch-match", javaPC + codeReader->readU4(), 1, 1);
+	llvm::BasicBlock* def = forwardBranch("lookupswitch-default", javaPC + codeReader->readU4(), 1, 1);
 	uint32_t n = codeReader->readU4();
 	
 	for(uint32_t i=0; i<n; i++) {
@@ -716,6 +716,19 @@ void J3CodeGen::lookupSwitch() {
 	}
 }
 
+void J3CodeGen::tableSwitch() {
+	codeReader->seek(((codeReader->tell() - 1) & -4) + 4, J3Reader::SeekSet);
+	llvm::Value* val = stack.pop();
+	llvm::BasicBlock* def = forwardBranch("tableswitch-default", javaPC + codeReader->readU4(), 1, 1);
+	int32_t low = codeReader->readU4();
+	int32_t high = codeReader->readU4();
+	llvm::SwitchInst* dispatch = builder->CreateSwitch(val, def, high - low + 1);
+
+	for(uint32_t i=low; i<=high; i++)
+		dispatch->addCase(builder->getInt32(i),
+											forwardBranch("tableswitch-match", javaPC + codeReader->readU4(), 1, 1));
+}
+
 llvm::BasicBlock* J3CodeGen::newBB(const char* name) {
 	return llvm::BasicBlock::Create(llvmFunction->getContext(), name, llvmFunction);
 }
@@ -727,13 +740,14 @@ void J3CodeGen::condBr(llvm::Value* op)
 }
 
 llvm::BasicBlock* J3CodeGen::forwardBranch(const char* id, uint32_t pc, bool doAlloc, bool doPush) {
-	if(vm->options()->debugTranslate > 2)
-		fprintf(stderr, "        forward branch at %d\n", pc);
 	llvm::BasicBlock* res = opInfos[pc].bb;
 
 	if(res) 
 		return res;
 
+	if(vm->options()->debugTranslate > 2)
+		fprintf(stderr, "        forward branch at %d\n", pc);
+
 	if(opInfos[pc].insn) {
 		//printf("split at %d (%s)\n", pc, id);
 		llvm::Instruction* insn = opInfos[pc].insn;
@@ -1454,11 +1468,13 @@ void J3CodeGen::translate() {
 			case J3Cst::BC_jsr: nyi();                    /* 0xa8 */
 			case J3Cst::BC_ret: nyi();                    /* 0xa9 wide */
 			case J3Cst::BC_tableswitch:                   /* 0xaa */
-				lookupSwitch(); /* TODO, generate a better code */
+				tableSwitch();
+				_onEndPoint();
 				break;
 
 			case J3Cst::BC_lookupswitch: 
 				lookupSwitch();
+				_onEndPoint();
 				break;
 
 			case J3Cst::BC_ireturn:                       /* 0xac */





More information about the vmkit-commits mailing list