\documentclass[a4paper]{article} \usepackage[portuges]{babel} \usepackage{a4wide} \usepackage[latin1]{inputenc} \usepackage{epsf} \parindent=0pt \parskip=2pt \newtheorem{questao}{Quest\~{a}o} \def\site{\emph{site}} \def\internet{\textsf{Internet}} \def\sgml{\textsf{SGML}} \def\html{\textsf{HTML}} \def\dtd{\textsf{DTD}} \def\w3{\textsf{WWW}} \def\vm{\textsf{VM}} \title{Generating Code for the Virtual Machine \vm\\ A small example} \author{Daniela Carneiro da Cruz \and Pedro Rangel Henriques} \date{\{danieladacruz,prh\}@@di.uminho.pt\\ Departamento de Inform\'{a}tica, CCTC\\ Universidade do Minho} \begin{document} \maketitle The purpose of this document is twofold. On one hand we want to present an example of a \vm\ program in its assembly language; that program shows how to use a subset of assembly instructions, and illustrates their operational semantics.\\ On the other hand we intend to illustrate a translation scheme from a high-level programming language to assembly code. %-------------------------------------------------------------------------- \section{A LISS Source Program} Suppose we have the following program, written in the high-level programming language \textsf{LISS} (\textbf{L}anguage of \textsf{I}ntegers, \textsf{S}equences and \textsf{S}ets), and we want to execute it.\\ For that it is necessary to translate that program into the code (assembly or binary) of a target machine. @o Integers.liss @{ program IntegerTest { Declarations intA := 4, intB, intC := 6 -> integer; c := [1,2,3], vector -> Array size 8; flag := false -> boolean; Statements intA = -3 + intC * (7 + c[1]); writeLn(c); c[5] = 1; /* logic operations */ bool = !( (intA == intB) || (intA != intC) && (c[1] < 6) ) || flag; } @} \section{Generated VM Code} To run the program above in the \vm\ virtual machine, it should be translated to \vm\ assembly code, either manual or automatically.\\ Bellow, we list the code generated by our \textsf{LissCompiler}. @o Integers.vm @{ // Line 6: intA := 4, intB, intC := 6 -> Integer PUSHI 4 PUSHI 0 PUSHI 6 // Line 7: c := [1,2,3], vector -> Array size 8 PUSHI 1 PUSHI 2 PUSHI 3 PUSHI 0 PUSHI 0 PUSHI 0 PUSHI 0 PUSHI 0 PUSHI 0 PUSHI 0 PUSHI 0 PUSHI 0 PUSHI 0 PUSHI 0 PUSHI 0 PUSHI 0 // Line 8: flag := false -> Boolean PUSHI 0 START // Line 13: intA = -3 + intC * (7 + c[1]) PUSHI -3 PUSHG 2 PUSHI 7 PUSHGP PUSHI 3 PADD PUSHI 1 PUSHI 1 MUL DUP 1 CHECK 0,7 LOADN ADD MUL ADD STOREG 0 @} @o Integers.vm @{ // Line 14: writeLn(c) PUSHS "]" PUSHG 10 STRI PUSHS "," PUSHG 9 STRI PUSHS "," PUSHG 8 STRI PUSHS "," PUSHG 7 STRI PUSHS "," PUSHG 6 STRI PUSHS "," PUSHG 5 STRI PUSHS "," PUSHG 4 STRI PUSHS "," PUSHG 3 STRI PUSHS "[" CONCAT CONCAT CONCAT CONCAT CONCAT CONCAT CONCAT CONCAT CONCAT CONCAT CONCAT CONCAT CONCAT CONCAT CONCAT CONCAT WRITES @} @o Integers.vm @{ // Line 15: c[5] = 1 PUSHGP PUSHI 3 PADD PUSHI 5 PUSHI 1 MUL DUP 1 CHECK 0,7 PUSHI 1 STOREN // Line 19: bool = !((intA == intB) || (intA != intC) && (c[1] < 6)) || flag PUSHG 0 PUSHG 1 EQUAL PUSHG 0 PUSHG 2 EQUAL NOT PUSHGP PUSHI 3 PADD PUSHI 1 PUSHI 1 MUL DUP 1 CHECK 0,7 LOADN PUSHI 6 INF AND OR NOT PUSHG 20 OR STOREG 19 STOP @} \section{Files in document} @f \end{document}