字节码解释器如何知道运行时错误在上面发生了什么行?

发布于 2025-02-01 12:01:39 字数 145 浏览 3 评论 0原文

截至目前,我正在使用一种将委员会编译到字节码的语言,然后由VM运行。我的问题是,当发生运行时错误时,VM如何知道源代码的哪一行引起了错误,因为在编译过程中删除了所有空格。我想到的一件事是存储一个与字节码与其中的行号相关联的单独的整数数组,但这听起来很大,尤其是在有很多说明时。

As of now, I am working on a language that compiles to bytecode, and then is ran by a VM. My question is, when a runtime error occurs, how does the VM know what line of the source code caused the error, as all whitespace is removed during the compilation process. One thing I would think of is to store a separate array of integers correlating to the bytecode with the line numbers within it, but that sounds extremely memory-inefficient, especially when there are a lot of instructions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀中猫帐中妖 2025-02-08 12:01:39

某些形式的字节码包含有关行号,方法名称等的信息,其中包括提供更好的调试信息。例如,在JVM中,方法字节座包含一个表,该表将字节地址的范围映射到源行号。这是存储它的一种更有效的方法,而不是用行号标记每个字节码操作,因为每行通常有多个操作。它确实使用了额外的空间,尽管我不会将其分类为极低的效率。

没有此信息,口译员实际上没有办法报告有关原始程序的任何方法,因为您已经注意到所有这些信息已被丢弃。

这类似于编译的可执行文件处理调试信息的方式。包含调试符号,该程序具有表映射代码地址到功能名称和行​​号。符号被剥离后,您只有原始说明和数据,并且无法参考原始代码。

Some forms of bytecode contain information about line numbers, method names, etc. which are included to provide better debugging information. In the JVM, for example, method bytecode contains a table that maps ranges of bytecode addresses to source line numbers. That’s a more efficient way of storing it than tagging each bytecode operation with a line number, since there are typically multiple operations per line. It does use extra space, though I wouldn’t classify it as extremely inefficient.

Absent this info, there really isn’t a way for the interpreter to report anything about the original program, since as you’ve noted all that information is otherwise discarded.

This is similar to how compiled executables handle debug info. With debug symbols included, the program has tables mapping code addresses to function names and line numbers. With symbols stripped out, you just have raw instructions and data and there’s no way to reference the original code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文