next up previous
Next: Example1 Up: Virtual Machine for CS394 Previous: Virtual Machine Data Structures

Virtual Machine Instructions

  1. Machine instructions are C assignment statements restricted to the following grammar:

    instruction $\rightarrow$ unlabeled_instruction $\vert$ labeled_instruction
    labeled_instruction $\rightarrow$ label : unlabeled_instruction
    label $\rightarrow$ identifier
    unlabeled_instruction $\rightarrow$ lhs = rhs $\vert$ push(pushargument) $\vert$ if lhs goto label
    unlabeled_instruction $\rightarrow$ goto label $\vert$ pop() $\vert$ procname() $\vert$ return $\vert$ putint(putintargument)
    pushargument $\rightarrow$ lhs $\vert$ smallterm
    lhs $\rightarrow$ stack[term] $\vert$ term $\vert$ register
    term $\rightarrow$ stack[smallterm]
    smallterm $\rightarrow$ register + const $\vert$ register - const $\vert$ register
    register $\rightarrow$ R$_i$ $\vert$ BP $\vert$ SP
    const $\rightarrow$ integer
    rhs $\rightarrow$ const $\vert$ lhs $\vert$ register op lhs $\vert$ register op const $\vert$ pop() $\vert$ getint()
    putintargument $\rightarrow$ rhs $\vert$ smallterm
    op $\rightarrow$ + $\vert$ - $\vert$ * $\vert$ % $\vert$ / $\vert$ < $\vert$ =< $\vert$ > $\vert$ >= $\vert$ == $\vert$ !=

    There is a restriction that if the rhs is register op lhs or register op const, then lhs must be the same register name as in regsiter.

  2. Instructions are combined using ; and are grouped in parameterless procedures. There must be one parameterless function func for each function func in the source program. Note that this function cannot return any value.

  3. push , pop , getint and putint are defined as:
    void push(int element)
    {
        stack[++SP]=element;
        return;
    }
    int pop()
    {
        return(stack[SP--]);
    }
    int getint()
    {
        int a;
        scanf("%d",&a);
        return a;
    }
    void putint(int a)
    {
        printf("%d",a);
        return;
    }
    


next up previous
Next: Example1 Up: Virtual Machine for CS394 Previous: Virtual Machine Data Structures
2006-03-18