Komphyler
HamidReza Kamkari 97110177
Mostafa Ojaghi 97105782
Yeganeh Gharedaghi 97106216
Input and Output: The following commands handle simple input and output of character.
Input x
: Takes an Integer as an input and writes it inx
Output x
: Outputs the value inx
.
Assign value commands:
Assign x = y
: Set the value ofx
equal to the value ofy
Assign x = a op b
: This command calculatesa op b
and then writes the output in x.op
can have forms such as follows:+
: Adda
andb
-
: Subtractb
froma
*
: Multiplya
andb
/
: Calculatea
divided byb
%
: Calculatea
modb
<
: Ifa
is strictly less thanb
then the value1
will be written intox
otherwise0
<=
: Ifa
is less than or equal tob
then the value1
will be written intox
otherwise0
>
: Ifa
is strictly larger thanb
then the value1
will be written intox
otherwise0
>=
: Ifa
is larger than or equal tob
then the value1
will be written intox
otherwise0
==
: Ifa
andb
are equal then1
will be written intox
otherwise0
!=
: Ifa
andb
are not equal then1
will be written intox
otherwise0
&&
: Calculate logical and ofa
andb
(a
andb
should be0
or1
)||
: Calculate logical or ofa
andb
(a
andb
should be0
or1
)
Assign x = not y
:y
should be0
or1
and the calculated not is written intox
Assign x = - y
: negatey
and then write the negated value intox
Labels and goto:
label labelName:
: defines a label to be able to jump to.goto labelName
: A simple jump to label namedlabelName
Function call commands:
label functionName: param1 param2 param3 ...
: This defines a label, if the label is a function the parametersparam1
,param2
, ... are the input arguments.Lcall functionName
: Call the function that has a label namedfunctionName
Lcall functionName -> r
: Call the function that has a label namedfunctionName
and then return the value from that function inr
Pushparam x
: This adds argumentx
to the stack before function call.Popparams sz
: Free the stackBeginfunc sz
: Free the stack bysz
bytes, this is used to free space for each local variable that is used in the function. Caution: keep in mind if you have 3 variables in a function you should start the function withBeginfunc 12
.Endfunc
: ends the function.Endfunc var
: returns the value that is stored invar
Exit command:
Exit
: If this line is read then the program exits.
Global Variables: each program in TAC is written as follows:
Assign g1 = 0
Assign g2 = 0
Assign g3 = 0
.
.
.
Assign gk = 0
Lcall main
Exit
label main:
Beginfunc s
.
.
.
Endfunc
label func1: arg1 arg2 ...
Beginfunc s1
.
.
.
Endfunc ret1/nothing
label func2: arg1 arg2 ...
.
.
.
The variables g1
, g2
, ..., gk
are all global variables and the function
main
is the function that is being called. Other procedures such as func1
, func2
...
are all functions that may or may not return value. To call each of these functions an arbitrary amount of
Pushparam x
may be needed if they have input arguments and then after each function call there should
be a Popparams sz
.
Pointers: The following commands include memory access.
Store *(a + offset) = b
: Store the value ofb
in offset bytes aftera
in memory.Load a = *(b + offset)
: Load the value in addesb + offset
of memory
##TAC tests
Simple function call example:
Assign global1 = 10
Assign global2 = 20
Lcall main
Exit
label main:
Assign x = 0
Assign y = 1
Assign z = 2
Pushparam x
Pushparam y
Pushparam z
Lcall function -> r
Popparams 12
label function: z y x
Beginfunc 4
Assign s = x + y
Assign s = s - z
Assign s = s + global1
Endfunc s
Lcall main
Exit
Label main:
Beginfunc 64
Input s
Output s
Input a
Output a
Assign b = s / a
Assign b = - s
Output b
Pushparam b
Lcall f -> x
Popparams 4
Output x
Endfunc
Label f: c
Beginfunc 64
Output c
Lcall check
Endfunc c
Label check:
Beginfunc 64
Input y
Output y
Endfunc