Skip to main content
Log in

Mnemonics: type-safe bytecode generation at run time

  • Published:
Higher-Order and Symbolic Computation

Abstract

Mnemonics is a Scala library for generating method bodies in JVM bytecode at run time. Mnemonics supports a large subset of the JVM instructions, for which the static typing of the generator guarantees the well-formedness of the generated bytecode.

The library exploits a number of advanced features of Scala’s type system (type inference with bounded polymorphism, implicit parameters, and reflection) to guarantee that the compiler only accepts legal combinations of instructions at compile time. Additional instructions are supported at the price of a check at run time of the generator. In either case, bytecode verification of generated code is guaranteed to succeed.

This is a preview of subscription content, log in via an institution to check access.

Access this article

Price excludes VAT (USA)
Tax calculation will be finalised during checkout.

Instant access to the full article PDF.

Similar content being viewed by others

References

  1. Bawden, A.: Quasiquotation in Lisp. In: Danvy, O. (ed.) Proceedings ACM SIGPLAN Workshop on Partial Evaluation and Semantics-Based Program Manipulation PEPM’99, San Antonio, Texas, USA, pp. 4–12 (1999). BRICS Notes NS-99-1

    Google Scholar 

  2. bcel: Homepage of BCEL. http://jakarta.apache.org/bcel/index.html (2011)

  3. Bruneton, E.: ASM 3.0—A Java Bytecode Engineering Library. http://download.forge.objectweb.org/asm/asm-guide.pdf (2007)

  4. Cardelli, L., Wegner, P.: On understanding types, data abstraction, and polymorphism. Comput. Surv. 17, 471–522 (1985)

    Article  Google Scholar 

  5. Chiba, S., Nishizawa, M.: An easy-to-use toolkit for efficient Java bytecode translators. In: GPCE’03: Proceedings of the 2nd International Conference on Generative Programming and Component Engineering, Erfurt, Germany, pp. 364–376 (2003)

    Google Scholar 

  6. Consel, C., Lawall, J., Marlet, R.: Tempo Specializer—Users’ manual. http://phoenix.labri.fr/software/tempo/doc/tempo-doc-user.html (1998)

  7. Czarnecki, K., O’Donnell, J.T., Striegnitz, J., Taha, W.: DSL implementation in MetaOCaml, template Haskell, and C++. In: [18], pp. 51–72 (2003)

    Google Scholar 

  8. Fluet, M., Pucella, R.: Phantom types and subtyping. J. Funct. Program. 16(6), 751–791 (2006)

    Article  MathSciNet  MATH  Google Scholar 

  9. Freund, S.N., Mitchell, J.C.: A type system for object initialization in the Java bytecode language. ACM Trans. Program. Lang. Syst. 21(6), 1196–1250 (1999)

    Article  Google Scholar 

  10. Freund, S.N., Mitchell, J.C.: A type system for the Java bytecode language and verifier. J. Autom. Reason. 30(3–4), 271–321 (2003)

    Article  MATH  Google Scholar 

  11. Grant, B., Mock, M., Philipose, M., Chambers, C., Eggers, S.J.: DyC: an expressive annotation-directed dynamic compiler for C. Theor. Comput. Sci. 248(1–2), 147–199 (2000)

    Article  MATH  Google Scholar 

  12. Jones, M.P.: The functions of Java bytecode. In: Eisenbach, S. (ed.) Formal Underpinnings of Java (1998)

    Google Scholar 

  13. Kfoury, A.J., Wells, J.B.: A direct algorithm for type inference in the rank-2 fragment of the second-order lambda-calculus. In: Proceedings of the 1994 ACM Conference on Lisp and Functional Programming, Orlando, FL, USA, pp. 196–207 (1994)

    Chapter  Google Scholar 

  14. Kiselyov, O., Shan, C.C.: Lightweight static capabilities. Electron. Notes Theor. Comput. Sci. 174(7), 79–104 (2007)

    Article  Google Scholar 

  15. Kiselyov, O., Lämmel, R., Schupke, K.: Strongly typed heterogeneous collections. In: Haskell ’04: Proceedings of the ACM SIGPLAN workshop on Haskell, Snowbird, Utah, USA, pp. 96–107 (2004)

    Chapter  Google Scholar 

  16. Launchbury, J., Peyton Jones, S.L.: State in Haskell. Lisp Symb. Comput. 8(4), 293–341 (1995)

    Article  Google Scholar 

  17. Lee, P., Leone, M.: Optimizing ML with run-time code generation. In: Proceedings of the 1996 Conference on Programming Language Design and Implementation, Philadelphia, PA, USA, pp. 137–148 (1996)

    Chapter  Google Scholar 

  18. Lengauer, C., Batory, D.S., Consel, C., Odersky, M. (eds.): Domain-Specific Program Generation, International Seminar, Dagstuhl, Germany. Lecture Notes in Computer Science, vol. 3016. Springer, Berlin (2004)

    Google Scholar 

  19. Lindholm, T., Yellin, F.: The Java(tm) Virtual Machine Specification, 2nd edn. Addison-Wesley, Reading (1999)

    Google Scholar 

  20. Mainland, G., Morrisett, G., Welsh, M.: Flask: staged functional programming for sensor networks. In: Thiemann, P. (ed.) Proceedings of the 13th ACM SIGPLAN International Conference on Functional Programming 2008, Victoria, BC, Canada, pp. 335–346 (2008)

    Chapter  Google Scholar 

  21. Morris, J.H. Jr.: Protection in programming languages. Commun. ACM 16(1), 15–21 (1973)

    Article  MATH  Google Scholar 

  22. Morrisett, J.G., Crary, K., Glew, N., Walker, D.: Stack-based typed assembly language. In: TIC’98: Proceedings of the Second International Workshop on Types in Compilation, Kyoto, Japan. Lecture Notes in Computer Science, vol. 1473, pp. 28–52 (1998)

    Google Scholar 

  23. Nordenberg, J.: MetaScala. http://www.assembla.com/wiki/show/metascala (2008)

  24. Odersky, M., Altherr, P., Cremet, V., Dragos, I., Dubochet, G., Emir, B., McDirmid, S., Micheloud, S., Mihaylov, N., Schinz, M., Spoon, L., Stenman, E., Zenger, M.: An overview of the Scala programming language, 2nd edn. Technical report, EPFL Lausanne (2006)

  25. Odersky, M., Spoon, L., Venners, B.: Programming in Scala: A Comprehensive Step-by-step Guide, 1st edn. Artima Inc, Mountain View (2008)

    Google Scholar 

  26. Odersky, M., Zenger, C., Zenger, M.: Colored local type inference. In: Nielson, H.R. (ed.) Proceedings 28th Annual ACM Symposium on Principles of Programming Languages, London, England, pp. 41–53 (2001)

    Google Scholar 

  27. Oiwa, Y., Masuhara, H., Yonezawa, A.: DynJava: type safe dynamic code generation in Java. In: 3rd JSSST Workshop on Programming and Programming Languages (PPL2001) (2001)

    Google Scholar 

  28. Oury, N., Swierstra, W.: The power of Pi. In: Thiemann, P. (ed.) Proceedings of the 13th ACM SIGPLAN International Conference on Functional Programming, Victoria, BC, Canada, pp. 39–50 (2008)

    Chapter  Google Scholar 

  29. Poletto, M., Hsieh, W.C., Engler, D.R., Kaashoek, M.F.: ‘C and tcc: a language and compiler for dynamic code generation. ACM Trans. Program. Lang. Syst. 21(2), 324–369 (1999)

    Article  Google Scholar 

  30. Qian, Z.: A formal specification of Java virtual machine instructions for objects, methods and subroutines. In: Formal Syntax and Semantics of Java, pp. 271–312 (1998)

    Google Scholar 

  31. Reynolds, J.C.: Types, abstraction, and parametric polymorphism. In: Mason, R.E.A. (ed.) Information Processing’83, pp. 513–523 (1983)

    Google Scholar 

  32. Rompf, T., Odersky, M.: Lightweight modular staging: a pragmatic approach to runtime code generation and compiled DSLs. In: Visser, E., Järvi, J. (eds.) Proceedings of the Ninth International Conference on Generative Programming and Component Engineering, GPCE 2010, Eindhoven, The Netherlands, pp. 127–136 (2010)

    Chapter  Google Scholar 

  33. Rose, E.: Lightweight bytecode verification. J. Autom. Reason. 31(3–4), 303–334 (2003)

    Article  MATH  Google Scholar 

  34. Rudolph, J.: Mnemonics: type-safe bytecode combination in Scala. Diploma thesis, Albert-Ludwigs-Universität Freiburg. http://virtual-void.net/files/mnemonics.zip (2009)

  35. Rudolph, J., Thiemann, P.: Mnemonics: type-safe bytecode generation at run time. In: Gallagher, J.P., Voigtländer, J. (eds.) Proceedings of the 2010 ACM SIGPLAN Workshop on Partial Evaluation and Program Manipulation, PEPM 2010, Madrid, Spain, pp. 15–24 (2010)

    Chapter  Google Scholar 

  36. Stata, R., Abadi, M.: A type system for Java bytecode subroutines. ACM Trans. Program. Lang. Syst. 21(1), 90–137 (1999)

    Article  Google Scholar 

  37. Taha, W.: A sound reduction semantics for untyped CBN multi-stage computation. In: Lawall, J. (ed.) Proceedings ACM SIGPLAN Workshop on Partial Evaluation and Semantics-Based Program Manipulation PEPM’00, Boston, MA, USA (2000)

    Google Scholar 

  38. Taha, W.: A gentle introduction to multi-stage programming. In: [18], pp. 30–50 (2004)

    Google Scholar 

  39. Taha, W., Nielsen, M.F.: Environment classifiers. In: Morrisett, G. (ed.) Proceedings 30th Annual ACM Symposium on Principles of Programming Languages, New Orleans, LA, USA, pp. 26–37 (2003)

    Google Scholar 

  40. Yelland, P.M.: A compositional account of the Java virtual machine. In: Aiken, A. (ed.) Proceedings 26th Annual ACM Symposium on Principles of Programming Languages, San Antonio, Texas, USA, pp. 57–69 (1999)

    Google Scholar 

Download references

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Peter Thiemann.

Rights and permissions

Reprints and permissions

About this article

Cite this article

Rudolph, J., Thiemann, P. Mnemonics: type-safe bytecode generation at run time. Higher-Order Symb Comput 23, 371–407 (2010). https://doi.org/10.1007/s10990-011-9077-9

Download citation

  • Published:

  • Issue Date:

  • DOI: https://doi.org/10.1007/s10990-011-9077-9

Keywords

Navigation