---
title: "jvm interpreter"
author: "cooldatabase"
date: "2010-04-06T23:51:12Z"
tags: ["JNI", "JVM"]
categories: ["非技术"]
slug: "jvm-interpreter"
draft: false
lastmod: "2010-04-07T08:54:51Z"
id: 634978
url: "https://cooldatabase.iteye.com/blog/634978"
---

jvm interpreter 解释器的初始化 JNI\_CreateJavaVM | |--&gt; Threads::create\_vm | |--&gt; init\_globals | |--&gt;interpreter\_init | |--&gt;AbstractInterpreter::initialize | |--&gt;TemplateTable::initialize 一个解释器就是不断地读取当前的指令，然后是一个大的switch语句。 传统解释器技术，是指对于java的bytecode，仍是其处理的基本单位。但是对于每个bytecode 的实现，则是采用的汇编技术。 void TemplateTable::iconst(int value) { transition(vtos, itos); if (value == 0) { \__ xorl(eax, eax); } else { \__ movl(eax, value); } } TemplateTable是解释器对这种技术的一个称呼。每一个java bytecode对应一个Template， 所有的Template构成TemplateTable。 上面的代码就是iconst\_*的实现。 有几点要注意 一、方法TemplateTable::iconst的实现和CPU相关。上面的代码实际上是intel CPU下的代码， 其位于：\\hotspot\\src\\cpu\\i486\\vm\\templateTable\_i486.cpp 二、\__ xorl(eax, eax); 中的 \__ 是什么？ #define \__ \_masm-&gt; 所以 \__ xorl(eax, eax); &lt;==&gt; \_masm-&gt;xorl(eax, eax); 而\_masm的定义是 static InterpreterMacroAssembler* \_masm; (templateTable.hpp line92) 三、InterpreterMacroAssembler的实现又是和CPU相关的。 InterpreterMacroAssembler的父类是：Assembler，它的实现也是和CPU相关的。 下面是xorl的一个实现 void Assembler::xorl(Register dst, Register src) { emit\_arith(0x33, 0xC0, dst, src); } emit\_arith的调用了emit\_byte方法，而emit\_byte的核心代码是： inline void AbstractAssembler::emit\_byte(int x) { // ... \*(unsigned char\*)\_code\_pos = (unsigned char)x; \_code\_pos += sizeof(unsigned char); // ... } 其中的\_code\_pos是一个内存缓冲区。 0x33 0xC0对应的Intel汇编语言正是：xor eax, eax*