logo Use CA10RAM to get 10%* Discount.
Order Nowlogo
(5/5)

if(t == T_DOUBLE && y->s_mode != T_DOUBLE){ return (gen(\"cv\", (struct sem_rec *) NULL, y, t));

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

I have 2 areas that say HERE in my file that I need help with so I can compile it and run test it

# include <stdio.h>

# include <stdlib.h>

# include "cc.h"

# include "scan.h"

# include "semutil.h"

# include "sem.h"

# include "sym.h"

 

#include <string.h>

#define MAXLOOPS 500

 

// Forward declare the error function so gcc will shut up

extern void yyerror(char *s);

 

// Tracks formal parameters and their types in the current scope level

extern int formalnum;

extern char formaltypes[];

 

// Tracks local variables and their types in the current scope level

extern int localnum;

extern char localtypes[];

 

// Tracks widths of locals 

extern int localwidths[];

 

int numlabels = 0;                      /* total labels in file */

int numblabels = 0;                     /* total backpatch labels in file */

int numloops = 0; // Track how many times we have looped

struct sem_rec* loops[MAXLOOPS]; // Track n for patching

 

 

int infunc = 0; // If we are in a function, we need to track the type

// to handle implicit returns of

// doubles

int functype = 0;

 

void deepcopy(struct sem_rec *, struct sem_rec *); // Local helper

 

 

/*

 * backpatch - backpatch list of quadruples starting at p with k

 */

void backpatch(struct sem_rec *p, int k)

{

struct sem_rec *curr = p;

while(NULL != curr)

{

printf("B%d=L%d\n", p->s_place, k);

curr = curr->back.s_link;

}

}

 

 

/*

 * bgnstmt - encountered the beginning of a statement

 */

void bgnstmt()

{

extern int lineno;

skip();

printf("bgnstmt %d\n", lineno);

}

 

 

/*

 * call - procedure invocation

 */

struct sem_rec *call(char *f, struct sem_rec *args)

{

struct sem_rec *curr = args;

int numargs = 0;

while(NULL != curr)

{

gen("arg", (struct sem_rec *)NULL, curr, curr->s_mode);

numargs++;

curr = curr->back.s_link;

}

struct id_entry *p;

if((p = lookup(f, 0)) == NULL)

{

p = install(f, 0);

p->i_type = T_PROC;

p->i_scope = GLOBAL;

p->i_defined = 1;

}

//HERE

printf("t%d := global %s\n", nexttemp(), f);

return gen("f",

   node(currtemp(), 0, (struct sem_rec *)NULL, (struct sem_rec *)NULL),

   node(numargs, 0, (struct sem_rec *)NULL, (struct sem_rec *)NULL),

   p->i_type);

}

 

 

/*

 * ccand - logical and

 */

struct sem_rec *ccand(struct sem_rec *e1, int m, struct sem_rec *e2)

{

backpatch(e1->back.s_true, m);

return node(0, 0, e2->back.s_true, merge(e1->s_false, e2->s_false));

}

 

 

/*

 * ccexpr - convert arithmetic expression to logical expression

 */

struct sem_rec *ccexpr(struct sem_rec *e)

{

struct sem_rec *t1;

if(e)

{

int realtype = e->s_mode &~ T_ADDR;

t1 = gen("!=", e, cast(con("0"), realtype), realtype);

numblabels++;

printf("bt t%d B%d\n", t1->s_place, numblabels);

numblabels++;

printf("br B%d\n", numblabels);

return node(0,

     0,

     node(numblabels-1, 0, (struct sem_rec *) NULL, (struct sem_rec *) NULL),

     node(numblabels, 0, (struct sem_rec *) NULL, (struct sem_rec *) NULL)

);

}

else

{

fprintf(stderr, "Argument sem_rec is NULL\n");

}

}

 

 

/*

 * ccnot - logical not

 */

struct sem_rec *ccnot(struct sem_rec *e)

{

return node(0, 0, e->s_false, e->back.s_true);

}

 

 

/*

 * ccor - logical or

 */

struct sem_rec *ccor(struct sem_rec *e1, int m, struct sem_rec *e2)

{

backpatch(e1->s_false, m);

return node(0, 0, merge(e1->back.s_true, e2->back.s_true), e2->s_false);

}

 

 

/*

 * con - constant reference in an expression

 */

struct sem_rec *con(char *x)

{

struct id_entry *p;

if((p = lookup(x, 0)) == NULL)

{

p = install(x, 0);

p->i_type = T_INT;

p->i_scope = GLOBAL;

p->i_defined = 1;

}

printf("t%d := %s\n", nexttemp(), x);

return(node(currtemp(), p->i_type, (struct sem_rec *) NULL, (struct sem_rec *) NULL));

}

 

 

/*

 * dobreak - break statement

 */

void dobreak()

{

numblabels++;

printf("br B%d\n", numblabels);

struct sem_rec *curr = loops[numloops]->s_false;

 

if(NULL != curr)

{

while(NULL != curr->back.s_link)

{

curr = curr->back.s_link;

}

}

curr = node(numblabels, 0, (struct sem_rec *)NULL, (struct sem_rec *)NULL);

}

 

 

/*

 * docontinue - continue statement

 */

void docontinue()

{

numblabels++;

printf("br B%d\n", numblabels);

struct sem_rec *curr = loops[numloops]->back.s_true;

if(NULL != curr)

{

while(NULL != curr->back.s_link)

{

curr = curr->back.s_link;

}

}

curr = node(numblabels, 0, (struct sem_rec *)NULL, (struct sem_rec *)NULL);

}

 

 

/*

 * dodo - do statement

 */

void dodo(int m1, int m2, struct sem_rec *e, int m3)

{

backpatch(e->back.s_true, m1);

backpatch(e->s_false, m3);

backpatch(loops[numloops]->back.s_true, m2);

endloopscope(m3);

}

 

 

/*

 * dofor - for statement

 */

void dofor(int m1, struct sem_rec *e2, int m2, struct sem_rec *n1, int m3,

   struct sem_rec *n2, int m4)

{    

backpatch(e2->back.s_true, m3);

backpatch(e2->s_false, m4);

backpatch(n1, m1);

backpatch(n2, m2);

backpatch(loops[numloops]->back.s_true, m2);

endloopscope(m4);

}

 

 

/*

 * dogoto - goto statement

 */

void dogoto(char *id){

printf("br L%s\n", slookup(id));

}

 

 

/*

 * doif - one-arm if statement

 */

void doif(struct sem_rec *e, int m1, int m2){

 

backpatch(e->back.s_true, m1);

backpatch(e->s_false, m2);

}

 

 

/*

 * doifelse - if then else statement

 */

void doifelse(struct sem_rec *e, int m1, struct sem_rec *n,

      int m2, int m3)

{

backpatch(e->back.s_true, m1);

backpatch(e->s_false, m2);

backpatch(n, m3);

}

 

 

/*

 * doret - return statement

 * 

 */

void doret(struct sem_rec *e)

{

if(NULL != e)

{

gen("ret", (struct sem_rec *)NULL, e,  e->s_mode);

}

else

{

if(functype & T_DOUBLE)

{

printf("retf\n");

}

else

{

printf("reti\n");

}

}

}

 

 

/*

 * dowhile - while statement

 */

void dowhile(int m1, struct sem_rec *e, int m2, struct sem_rec *n,

     int m3)

{

backpatch(e->back.s_true, m2);

backpatch(e->s_false, m3);

backpatch(n, m1);

backpatch(loops[numloops]->back.s_true, m1);

endloopscope(m3);

}

 

 

/*

 * endloopscope - end the scope for a loop

 */

void endloopscope(int m)

{

backpatch(loops[numloops]->s_false, m);

loops[numloops] = NULL;

numloops--;

leaveblock();

}

 

 

/*

 * exprs - form a list of expressions

 */

struct sem_rec *exprs(struct sem_rec *l, struct sem_rec *e)

{

return merge(l,e);

}

 

 

/*

 * fhead - beginning of function body

 */

void fhead(struct id_entry *p){

int i;

//HERE

/*

struct id_entry *t;

t = lookup(p->i_name,0);

//printf("%s", t);

printf("%s", p->i_name);

/*

for(i = 0; i<1; i+=1){

printf("%s", localtypes[i]);

}

printf("%s", *p);

*/

for(i = 0; i<formalnum; ++i){

if('i' == formaltypes[i]){

printf("formal 1 4\n");

}

else{

printf("formal  4 8\n");

}

}

 

for(i = 0; i<localnum; ++i){

if('i' == localtypes[i]){

printf("localloc  1 4\n");

}

else{

printf("localloc  4 8\n");

}

}

}

 

 

/*

 * fname - function declaration

 */

struct id_entry *fname(int t, char *id){

int width;

infunc = 1;

switch(t){

case T_INT:

width = 4;

functype = T_INT;

break;

default:

width = 8;

functype = T_DOUBLE;

}

printf("func %s %d\n", id, t);

formalnum = 0;

localnum = 0;

 

struct id_entry * ret = dclr(id, t, width);

 

 

enterblock();

 

if(NULL != ret){

return ret;

}

yyerror("function declaration returned null");

return (struct id_entry *) NULL;

}

 

 

/*

 * ftail - end of function body

 */

void ftail(){

printf("fend\n");

leaveblock();

infunc = 0;

}

 

 

/*

 * id - variable reference

 */

struct sem_rec *id(char *x){

struct id_entry *p;

if((p = lookup(x, 0)) == NULL){

yyerror("undeclared identifier");

p = install(x, -1);

p->i_type = T_INT;

p->i_scope = LOCAL;

p->i_defined = 1;

}

if (p->i_scope == GLOBAL){

printf("t%d := global %s\n", nexttemp(), x);

}

else if(p->i_scope == LOCAL){

printf("t%d := local %s %d\n", nexttemp(), x, p->i_offset);

}

else if(p->i_scope == PARAM){

printf("t%d := param %s %d\n", nexttemp(), x, p->i_offset);

if(p->i_type & T_ARRAY){

    (void) nexttemp();

printf("t%d := @i t%d\n", currtemp(), currtemp()-1);

}

}

 

return node( currtemp(),

p->i_type|T_ADDR,

(struct sem_rec *) NULL,

(struct sem_rec *) NULL

);

}

 

 

/*

 * index - subscript

 */

struct sem_rec *sindex(struct sem_rec *x, struct sem_rec *i){

 

return (gen("[]", x, cast(i, T_INT), x->s_mode&~(T_ARRAY)));

}

 

 

/*

 * labeldcl - process a label declaration

 */

void labeldcl(char *id){

slookup(id);

numlabels++;

printf("label L%d\n", numlabels);

}

 

 

/*

 * m - generate label and return next temporary number

 */

int m(){

numlabels++;

printf("label L%d\n", numlabels);

return numlabels;

}

 

 

/*

 * n - generate goto and return backpatch pointer

 */

struct sem_rec *n(){

numblabels++;

printf("br B%d\n", numblabels);

return node(numblabels, 0, (struct sem_rec *)NULL, (struct sem_rec *)NULL);

}

 

 

/*

 * op1 - unary operators

 */

struct sem_rec *op1(char *op, struct sem_rec *y){

 

if(*op == '@' && !(y->s_mode&T_ARRAY)){

y->s_mode &= ~T_ADDR;

return (gen(op, (struct sem_rec *) NULL, y, y->s_mode));

}

else{

return gen(op, (struct sem_rec *)NULL, y, y->s_mode);

}

}

 

 

/*

 * op2 - arithmetic operators

 */

struct sem_rec *op2(char *op, struct sem_rec *x, struct sem_rec *y){

struct sem_rec *p;

if(NULL != x){

if(x->s_mode & T_DOUBLE && y->s_mode & T_INT){

p = cast(y, x->s_mode);

deepcopy(p,y);

}

else if (x->s_mode & T_INT && y->s_mode & T_DOUBLE){

p = cast(x, y->s_mode);

deepcopy(p,x);

}

return gen(op,x,y,y->s_mode);

}

else{

return (struct sem_rec *)NULL;

}

}

 

 

/*

 * opb - bitwise operators

 */

struct sem_rec *opb(char *op, struct sem_rec *x, struct sem_rec *y){

 

struct sem_rec *p;

 

if(x->s_mode & T_DOUBLE || y->s_mode & T_DOUBLE){

yyerror("Double operands with bitwise operator");

return ((struct sem_rec *) NULL);

}

else{

return gen(op,x,y,T_INT);

}

}

 

 

/*

 * rel - relational operators

 */

struct sem_rec *rel(char *op, struct sem_rec *x, struct sem_rec *y){

 

int realtypex = x->s_mode & ~T_ADDR;

int realtypey = y->s_mode & ~T_ADDR;

 

struct sem_rec *p;

if(realtypex > realtypey){

p = cast(y, x->s_mode);

deepcopy(p,y);

}

else if(realtypey > realtypex){

p = cast(x, y->s_mode);

deepcopy(p,x);

}

 

struct sem_rec *ret = gen(op, x, y,y->s_mode);

 

numblabels++;

printf("bt t%d B%d\n", currtemp(), numblabels);

ret->back.s_true = node(numblabels, x->s_mode, (struct sem_rec *)NULL, (struct sem_rec *)NULL);

numblabels++;

ret->s_false = node(numblabels, x->s_mode, (struct sem_rec *)NULL, (struct sem_rec *)NULL);

printf("br B%d\n", numblabels);

return ret;

}

 

 

/*

 * set - assignment operators

 */

struct sem_rec *set(char *op, struct sem_rec *x, struct sem_rec *y){

struct sem_rec *p, *cast_y, *p2;

if(*op!='\0' || x==NULL || y==NULL){

int realtypex = x->s_mode & ~T_ADDR;

int realtypey = y->s_mode & ~T_ADDR;

if(1 == realtypex){

p = gen("@", (struct sem_rec *)NULL, x, T_INT);

cast_y = cast(y, T_INT);

}

else{

p = gen("@", (struct sem_rec *)NULL, x, T_DOUBLE);

cast_y = cast(y, T_DOUBLE);

}

 

p2 = gen(op, p, cast_y, cast_y->s_mode);

return gen("=", x, p2, cast_y->s_mode);

}

else{

cast_y = y;

 

int realtypex = x->s_mode & ~T_ADDR;

int realtypey = y->s_mode & ~T_ADDR;

if((x->s_mode & T_DOUBLE) && !(y->s_mode & T_DOUBLE)){

cast_y = cast(y, T_DOUBLE);

}

else if((x->s_mode & T_INT) && !(y->s_mode & T_INT)){

cast_y = cast(y, T_INT);

}

 

if((x->s_mode &~ T_ARRAY)& T_INT){

printf("t%d := t%d =i t%d\n", nexttemp(), x->s_place, cast_y->s_place);

}

else{

printf("t%d := t%d =f t%d\n", nexttemp(), x->s_place, cast_y->s_place);

}

}

 

return( node(currtemp(),

     (x->s_mode&~(T_ARRAY)),

     (struct sem_rec *)NULL,

     (struct sem_rec *)NULL)

);

}

 

 

/*

 * startloopscope - start the scope for a loop

 */

void startloopscope(){

 

numloops++;

struct sem_rec * curr = node(0, 0, (struct sem_rec *)NULL, (struct sem_rec *)NULL);

loops[numloops] = curr;

enterblock();

}

 

 

/*

 * string - generate code for a string

 */

struct sem_rec *string(char *s){

struct sem_rec *ret = node(nexttemp(), T_STR, (struct sem_rec *)NULL, (struct sem_rec *)NULL);

 

printf("t%d := %s\n", ret->s_place, s);

 

return ret;

}

 

 

/************* Helper Functions **************/

 

/*

 * cast - force conversion of datum y to type t

 */

struct sem_rec *cast(struct sem_rec *y, int t){

if(t == T_DOUBLE && y->s_mode != T_DOUBLE){

return (gen("cv", (struct sem_rec *) NULL, y, t));

}

else if(t != T_DOUBLE && y->s_mode == T_DOUBLE){

return (gen("cv", (struct sem_rec *) NULL, y, t));

}

else{

return (y);

}

}

 

 

/*

 * gen - generate and return quadruple "z := x op y"

 *

 * Would have been very nice to have instructions for this helper function.

 * Not so much about what it does, or what you did and did not intend this to

 * be used for.  The use was kind of inconsistent in the given code, and it

 * might have been better to separate this into several helper functions that

 * each handle one use pattern.

 *

 * As far as I understand it, we can use this one function for:

 * args

 * returns

 * general binary operations

 * making soup

 * certain unary operations

 * function calls

 * toasting marshmallows

 * the assignment of assignment operators

 * the operator of assignment operators

 */

struct sem_rec *gen(char *op, struct sem_rec *x, struct sem_rec *y, int t){

if(strncmp(op, "arg", 3) != 0 && strncmp(op, "ret", 3) != 0){

printf("t%d := ", nexttemp());

}

if(x != NULL && *op != 'f'){

   printf("t%d ", x->s_place);

}

printf("%s", op);

if(t & T_DOUBLE && (!(t & T_ADDR) || (*op == '[' && *(op+1) == ']'))) {

printf("f");

if(*op == '%'){

yyerror("cannot %% floating-point values");

}

}

else{

printf("i");

}

if(x != NULL && *op == 'f'){

printf(" t%d %d", x->s_place, y->s_place);

}

else if(y != NULL){

printf(" t%d", y->s_place);

}

printf("\n");

return (node(currtemp(), t, (struct sem_rec *) NULL, (struct sem_rec *) NULL));

}

 

 

void deepcopy(struct sem_rec *src, struct sem_rec* dest){

dest->s_place = src->s_place;

dest->s_mode = src->s_mode;

dest->back.s_link = src->back.s_link;

dest->s_false = src->s_false;

}

 

 

(5/5)
Attachments:

Related Questions

. Introgramming & Unix Fall 2018, CRN 44882, Oakland University Homework Assignment 6 - Using Arrays and Functions in C

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

. The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is an

Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t

. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual

Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

. SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

. Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Um e HaniScience

812 Answers

Hire Me
expert
Muhammad Ali HaiderFinance

806 Answers

Hire Me
expert
Husnain SaeedComputer science

958 Answers

Hire Me
expert
Atharva PatilComputer science

632 Answers

Hire Me

Get Free Quote!

409 Experts Online