大端字节序码流中取出2字节

在这篇文章中,我们将看到如何为我们的语言生成字节码。 到目前为止,我们已经看到了如何构建一种语言来表达我们想要的东西,如何验证该语言,如何为该语言构建编辑器,但实际上我们还不能运行代码。 是时候解决这个问题了。 通过为JVM进行编译,我们的代码将能够在各种平台上运行。 对我来说听起来很棒!

建立自己的语言的系列

以前的帖子:

  1. 建立一个词法分析器
  2. 建立一个解析器
  3. 创建带有语法突出显示的编辑器
  4. 使用自动补全功能构建编辑器
  5. 将解析树映射到抽象语法树
  6. 建模转换
  7. 验证方式

代码在GitHub上的标签为08_bytecode

添加打印声明

在跳入字节码生成之前,我们只需在我们的语言中添加一条打印语句即可。 这很容易:我们只需要在词法分析器和解析器定义中更改几行,就可以了。

// Changes to lexer
PRINT              : 'print';// Changes to parser
statement : varDeclaration # varDeclarationStatement| assignment     # assignmentStatement| print          # printStatement ;print : PRINT LPAREN expression RPAREN ;

我们的编译器的一般结构

让我们从编译器的入口点开始。 我们将从标准输入或文件中获取代码(将被指定为第一个参数)。 一旦获得代码,我们将尝试构建AST并检查词汇和语法错误。 如果没有,我们将验证AST并检查语义错误。 如果仍然没有错误,我们继续进行字节码生成。

fun main(args: Array<String>) {val code : InputStream? = when (args.size) {0 -> System.`in`1 -> FileInputStream(File(args[0]))else -> {System.err.println("Pass 0 arguments or 1")System.exit(1)null}}val parsingResult = SandyParserFacade.parse(code!!)if (!parsingResult.isCorrect()) {println("ERRORS:")parsingResult.errors.forEach { println(" * L${it.position.line}: ${it.message}") }return}val root = parsingResult.root!!println(root)val errors = root.validate()if (errors.isNotEmpty()) {println("ERRORS:")errors.forEach { println(" * L${it.position.line}: ${it.message}") }return}val bytes = JvmCompiler().compile(root, "MyClass")val fos = FileOutputStream("MyClass.class")fos.write(bytes)fos.close()
}

请注意,在此示例中,我们始终会生成一个名为MyClass的类文件。 大概以后,我们想找到一种为类文件指定名称的方法,但是现在这已经足够了。

使用ASM生成字节码

现在,让我们潜入有趣的部分。 JvmCompiler编译方法是我们生成字节的地方,以后我们将其保存到类文件中。 我们如何产生这些字节? 在ASM的帮助下,ASM是一个用于生成字节码的库。 现在,我们可以自己生成bytes数组,但要点是,它将涉及一些无聊的任务,例如生成类池结构。 ASM为我们做到了。 我们仍然需要对JVM的结构有所了解,但是我们可以生存下来而无需成为专家的精髓。

class JvmCompiler {fun compile(root: SandyFile, name: String) : ByteArray {// this is how we tell ASM that we want to start writing a new class. We ask it to calculate some values for usval cw = ClassWriter(ClassWriter.COMPUTE_FRAMES or ClassWriter.COMPUTE_MAXS)// here we specify that the class is in the format introduced with Java 8 (so it would require a JRE >= 8 to run)// we also specify the name of the class, the fact it extends Object and it implements no interfacescw.visit(V1_8, ACC_PUBLIC, name, null, "java/lang/Object", null)// our class will have just one method: the main method. We have to specify its signature// this string just says that it takes an array of Strings and return nothing (void)val mainMethodWriter = cw.visitMethod(ACC_PUBLIC or ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null)mainMethodWriter.visitCode()// labels are used by ASM to mark points in the codeval methodStart = Label()val methodEnd = Label()// with this call we indicate to what point in the method the label methodStart correspondsmainMethodWriter.visitLabel(methodStart)// Variable declarations:// we find all variable declarations in our code and we assign to them an index value// our vars map will tell us which variable name corresponds to which indexvar nextVarIndex = 0val vars = HashMap<String, Var>()root.specificProcess(VarDeclaration::class.java) {val index = nextVarIndex++vars[it.varName] = Var(it.type(vars), index)mainMethodWriter.visitLocalVariable(it.varName, it.type(vars).jvmDescription, null, methodStart, methodEnd, index)}// time to generate bytecode for all the statementsroot.statements.forEach { s ->when (s) {is VarDeclaration -> {// we calculate the type of the variable (more details later)val type = vars[s.varName]!!.type// the JVM is a stack based machine: it operated with values we have put on the stack// so as first thing when we meet a variable declaration we put its value on the stacks.value.pushAs(mainMethodWriter, vars, type)// now, depending on the type of the variable we use different operations to store the value// we put on the stack into the variable. Note that we refer to the variable using its index, not its namewhen (type) {IntType -> mainMethodWriter.visitVarInsn(ISTORE, vars[s.varName]!!.index)DecimalType -> mainMethodWriter.visitVarInsn(DSTORE, vars[s.varName]!!.index)else -> throw UnsupportedOperationException(type.javaClass.canonicalName)}}is Print -> {// this means that we access the field "out" of "java.lang.System" which is of type "java.io.PrintStream"mainMethodWriter.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;")// we push the value we want to print on the stacks.value.push(mainMethodWriter, vars)// we call the method println of System.out to print the value. It will take its parameter from the stack// note that we have to tell the JVM which variant of println to call. To do that we describe the signature of the method,// depending on the type of the value we want to print. If we want to print an int we will produce the signature "(I)V",// we will produce "(D)V" for a doublemainMethodWriter.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(${s.value.type(vars).jvmDescription})V", false)}is Assignment -> {val type = vars[s.varName]!!.type// This code is the same we have seen for variable declarationss.value.pushAs(mainMethodWriter, vars, type)when (type) {IntType -> mainMethodWriter.visitVarInsn(ISTORE, vars[s.varName]!!.index)DecimalType -> mainMethodWriter.visitVarInsn(DSTORE, vars[s.varName]!!.index)else -> throw UnsupportedOperationException(type.javaClass.canonicalName)}}else -> throw UnsupportedOperationException(s.javaClass.canonicalName)}}// We just says that here is the end of the methodmainMethodWriter.visitLabel(methodEnd)// And we had the return instructionmainMethodWriter.visitInsn(RETURN)mainMethodWriter.visitEnd()mainMethodWriter.visitMaxs(-1, -1)cw.visitEnd()return cw.toByteArray()}}

关于类型

好的,我们已经看到我们的代码使用类型。 这是必需的,因为根据类型,我们需要使用不同的说明。 例如,将值放入整数变量中,我们使用ISTORE;而将值放入双重变量中,我们使用DSTORE 。 当我们以整数调用System.out.println时,我们需要指定签名(I)V,而当我们调用它以打印双精度字符时,则需要指定(D)V

为此,我们需要了解每个表达式的类型。 在我们超简单的语言中,我们现在仅使用intdouble 。 在真实的语言中,我们可能想使用更多的类型,但这足以向您展示这些原理。

interface SandyType {// given a type we want to get the corresponding string used in the JVM// for example: int -> I, double -> D, Object -> Ljava/lang/Object; String -> [Ljava.lang.String;val jvmDescription: String
}object IntType : SandyType {override val jvmDescription: Stringget() = "I"
}object DecimalType : SandyType {override val jvmDescription: Stringget() = "D"
}fun Expression.type(vars: Map<String, Var>) : SandyType {return when (this) {// an int literal has type int. Easy :)is IntLit -> IntTypeis DecLit -> DecimalType// the result of a binary expression depends on the type of the operandsis BinaryExpression -> {val leftType = left.type(vars)val rightType = right.type(vars)if (leftType != IntType && leftType != DecimalType) {throw UnsupportedOperationException()}if (rightType != IntType && rightType != DecimalType) {throw UnsupportedOperationException()}// an operation on two integers produces integersif (leftType == IntType && rightType == IntType) {return IntType// if at least a double is involved the result is a double} else {return DecimalType}}// when we refer to a variable the type is the type of the variableis VarReference -> vars[this.varName]!!.type// when we cast to a value, the resulting value is that type :)is TypeConversion -> this.targetType.toSandyType()else -> throw UnsupportedOperationException(this.javaClass.canonicalName)}
}

表达方式

如我们所见,JVM是基于堆栈的计算机。 因此,每次我们想使用一个值时,都会将其压入堆栈,然后执行一些操作。 让我们看看如何将值推入堆栈

// Convert, if needed
fun Expression.pushAs(methodWriter: MethodVisitor, vars: Map<String, Var>, desiredType: SandyType) {push(methodWriter, vars)val myType = type(vars)if (myType != desiredType) {if (myType == IntType && desiredType == DecimalType) {methodWriter.visitInsn(I2D)} else if (myType == DecimalType && desiredType == IntType) {methodWriter.visitInsn(D2I)} else {throw UnsupportedOperationException("Conversion from $myType to $desiredType")}}
}fun Expression.push(methodWriter: MethodVisitor, vars: Map<String, Var>) {when (this) {// We have specific operations to push integers and double valuesis IntLit -> methodWriter.visitLdcInsn(Integer.parseInt(this.value))is DecLit -> methodWriter.visitLdcInsn(java.lang.Double.parseDouble(this.value))// to push a sum we first push the two operands and then invoke an operation which// depend on the type of the operands (do we sum integers or doubles?)is SumExpression -> {left.pushAs(methodWriter, vars, this.type(vars))right.pushAs(methodWriter, vars, this.type(vars))when (this.type(vars)) {IntType -> methodWriter.visitInsn(IADD)DecimalType -> methodWriter.visitInsn(DADD)else -> throw UnsupportedOperationException("Summing ${this.type(vars)}")}}is SubtractionExpression -> {left.pushAs(methodWriter, vars, this.type(vars))right.pushAs(methodWriter, vars, this.type(vars))when (this.type(vars)) {IntType -> methodWriter.visitInsn(ISUB)DecimalType -> methodWriter.visitInsn(DSUB)else -> throw UnsupportedOperationException("Summing ${this.type(vars)}")}}is DivisionExpression -> {left.pushAs(methodWriter, vars, this.type(vars))right.pushAs(methodWriter, vars, this.type(vars))when (this.type(vars)) {IntType -> methodWriter.visitInsn(IDIV)DecimalType -> methodWriter.visitInsn(DDIV)else -> throw UnsupportedOperationException("Summing ${this.type(vars)}")}}is MultiplicationExpression -> {left.pushAs(methodWriter, vars, this.type(vars))right.pushAs(methodWriter, vars, this.type(vars))when (this.type(vars)) {IntType -> methodWriter.visitInsn(IMUL)DecimalType -> methodWriter.visitInsn(DMUL)else -> throw UnsupportedOperationException("Summing ${this.type(vars)}")}}// to push a variable we just load the value from the symbol tableis VarReference -> {val type = vars[this.varName]!!.typewhen (type) {IntType -> methodWriter.visitVarInsn(ILOAD, vars[this.varName]!!.index)DecimalType -> methodWriter.visitVarInsn(DLOAD, vars[this.varName]!!.index)else -> throw UnsupportedOperationException(type.javaClass.canonicalName)}}// the pushAs operation take care of conversions, as neededis TypeConversion -> {this.value.pushAs(methodWriter, vars, this.targetType.toSandyType())}else -> throw UnsupportedOperationException(this.javaClass.canonicalName)}
}

Gradle

我们还可以创建gradle任务来编译源文件

main = "me.tomassetti.sandy.compiling.JvmKt"args = "$sourceFile"classpath = sourceSets.main.runtimeClasspath
}

结论

我们没有详细介绍,我们急于浏览代码。 我的目的只是给您概述用于生成字节码的一般策略。 当然,如果您想构建一种严肃的语言,则需要做一些研究并理解JVM的内部,这是无可避免的。 我只是希望这个简短的介绍足以使您了解到这并不那么令人恐惧或复杂,大多数人都认为。

翻译自: https://www.javacodegeeks.com/2016/09/generating-bytecode.html

大端字节序码流中取出2字节

大端字节序码流中取出2字节_产生字节码相关推荐

  1. 大端字节序码流中取出2字节_字节码忍者的秘密

    大端字节序码流中取出2字节 Java语言由Java语言规范(JLS)定义. 但是,Java虚拟机的可执行字节码由单独的标准Java虚拟规范(通常称为VMSpec)定义. JVM字节码由javac从Ja ...

  2. 大端字节序码流中取出2字节_graalvm字节码到位码

    大端字节序码流中取出2字节 GraalVM & Micronauts - frenzy way to MicroServices, Serverless: Part1 GraalVM和Micr ...

  3. tcp码流中查找rtp头_跟踪数据流中的时间以查找性能问题

    tcp码流中查找rtp头 We're facing a challenge with several of our data flows that use more time than they ha ...

  4. H264码流中SPS、PPS详解

    1 SPS和PPS从何处而来? 2 SPS和PPS中的每个参数起什么作用? 3 如何解析SDP中包含的H.264的SPS和PPS串? 1 客户端抓包 在做客户端视频解码时,一般都会使用Wireshar ...

  5. 【音视频数据数据处理 12】【H.264篇】解析H.264原始码流中的I帧 / P帧 / B帧数据(暂未解决,本文先放着,来日更新)

    [音视频数据数据处理 12][H.264篇]解析H.264原始码流中的I帧 / P帧 / B帧数据 一.如何判断是 I帧 / P帧 / B帧 1.1 slice_type 1.2 slice_head ...

  6. 对h.264压缩视频码流中i帧的提取(firstime)

    这个问题要说清楚还是有点复杂:首先判断 NALU 类型是否是 5,如果是,那么以后连续出现的 NALU 类型为 5 的 NALU 就属于 IDR 帧(一种特殊的 I 帧):如果 NALU 不是 5,则 ...

  7. 从h264码流中获取图像的宽高---版本2(简洁版)

    从264码流中获取图像的宽高,代码如下,注意代码文件应该为cpp文件 #include <stdio.h> #include <stdlib.h> #include <s ...

  8. H264码流中SPS的获取

    The h.264 Sequence Parameter Set April 20th, 2011 by Ben Mesander 此文对于想要了解如何获取h264码流中SPS参数的过程,但是又不是很 ...

  9. sps和pps一篇好的解释 H264码流中SPS PPS详解<转>

    https://blog.csdn.net/luzubodfgs/article/details/86775940 H264码流中NALU sps pps IDR帧的理解 https://blog.c ...

最新文章

  1. Python SimpleHTTPServer 简单开发
  2. matlab2013a vs2013 opencv2.4.8 编译TLD
  3. jquery ajax 省 城市 二级菜单 源码,利用了jquery的ajax实现二级联互动菜单
  4. 搭建LAMP下的ucenter家园博客
  5. 【开源框架】Android之史上最全最简单最有用的第三方开源库收集整理,有助于快速开发,欢迎各位网友补充完善...
  6. 人口简史:我们曾经差一点就彻底没了
  7. 联想开机启动项按哪个_联想电脑开机按f12后,怎么设置默认启动项
  8. PHP删除目录及目录下所有文件
  9. Win10如何关闭管理员权限运行
  10. 困难之下见证良心公司!!
  11. 一个在线排版小工具:中文、英文、数字、符号中间加个空格
  12. 基于C#的Windows控制台的吃豆豆小游戏
  13. 计算机word文档基本操作,Word常用基本操作
  14. gcc cross compiler 问题
  15. 编程训练题:多项式求和
  16. (五)图片压缩 —— 优化图片文件、内存
  17. photoshop cc2017全套视频课程 从基础到实战案例PS海报-王诚-专题视频课程
  18. w10能装inventor2019_Inventor2019下载
  19. 一张图让你搞懂“非对称加密”
  20. 计算机应用最普遍的汉字字符编码是什么,计算机中目前最普遍使用的汉字字符编码是什么...

热门文章

  1. 洛谷P3349:小星星(容斥dp)
  2. YBTOJ:彩球抽取(期望)
  3. 动态规划:openjudge 2.6-3532 最大上升子序列和 解题心得
  4. 51nod1667-概率好题【容斥,组合数学】
  5. P5137-polynomial【倍增】
  6. P4321-随机漫游【状压dp,数学期望,高斯消元】
  7. 欢乐纪中A组赛【2019.8.17】
  8. p2762-太空飞行计划问题【网络流,最大权闭合图,最小割】
  9. Mysql调优你不知道这几点,就太可惜了
  10. 互联网账户系统如何设计