int main(void){
	char str[100], input[100];

//	Prints Intro
	printf("Simple Calculator\n");

//	Prints the input prompt, along with goto label to jump back to input

	inputPrompt:
	printf(">>>");
	fgets(str,sizeof(str),stdin);
	sscanf(str, "%[^\n]",input);

//	printf("The input is: %s\n",input);


	/* Checks to see if the user wants to quit */
	if(strcmp(input, "q") == 0 || strcmp(input,"Q") == 0){
		printf(" Goodbye!");
		exit(0);
	}
	/* Checks to see if the user asked for help */
	else if(strcmp(input,"h") == 0 || strcmp(input,"H") == 0){
		printhelp();
	}

	/* If the string entered has passed all pre-checks, then send off to functions dealing with arithmetic */
	else{

		/* Send the user input to the function that will standardise the whitespaces to use as delimiters
		 * (make sure there is only ever one whitespace at most) */
		standardiseWhitespaces(input);
//		printf("After White spaces: %s\n",input);

		/* Run through a bunch of checks, print an error message and go back to the input prompt if something is found */
		if(operatorsIncorrect(input) == TRUE || tooManyPoints(input) == TRUE || invalidCharacters(input) == TRUE) {
			printf(" Error: Illegal input!\n");
			goto inputPrompt;
		}
		if(divideByZero(input) == TRUE){
			printf(" Error: Divide by zero!\n");
			goto inputPrompt;
		}

		/* Converts any e's to scientific notation */
		convertFromScientificNotation(input);
//		printf("After Scientific Notation: %s\n",input);

		/* Put string through function to turn it in to post-fix notation */
		convertToPostfix(input);

//		printf("After postfix: %s\n",input);

		/* Puts the postfix notation string in to the arithmetic function, which then interprets the equation, solves it , then prints it */
		printf(" %lf\n",arithmetic(input));
	}
	/* Goes to the start of the program where it prompts for input */
	goto inputPrompt;
}
Exemple #2
0
void main()
{
	char str[10] = {};
	gets(str);
	char outstr[10];
	int len = strlen(str);
	arithmetic(str,len,outstr);
	printf("%s\n",outstr);	
}
Exemple #3
0
void rnmatrix<T>::partAssign(const rnmatrix<T>& rnmat, const T rndata)
{
	if(rowDim < rnmat.getRowDim() || colDim < rnmat.getColDim())
		THROW std::runtime_error("partAssign: dimension mismatch");
	rnmatrix<T> rnmat_temp(rowDim, colDim, 0);
	mat = arithmetic(mat,rnmat_temp, '*') + rnmatrix(rowDim, colDim, rndata);
	for(size_type rnrow = 0; rnrow < rnmat.getRowDim(); ++rnrow)
		for(size_type rncol = 0; rncol < rnmat.getColDim(); ++rncol)
			mat[rnrow][rncol] = rnmat[rnrow][rncol];
}
Exemple #4
0
void FEComposite::platformApplySoftware()
{
    FilterEffect* in = inputEffect(0);
    FilterEffect* in2 = inputEffect(1);

    if (m_type == FECOMPOSITE_OPERATOR_ARITHMETIC) {
        ByteArray* dstPixelArray = createPremultipliedImageResult();
        if (!dstPixelArray)
            return;

        IntRect effectADrawingRect = requestedRegionOfInputImageData(in->absolutePaintRect());
        RefPtr<ByteArray> srcPixelArray = in->asPremultipliedImage(effectADrawingRect);

        IntRect effectBDrawingRect = requestedRegionOfInputImageData(in2->absolutePaintRect());
        in2->copyPremultipliedImage(dstPixelArray, effectBDrawingRect);

        arithmetic(srcPixelArray.get(), dstPixelArray, m_k1, m_k2, m_k3, m_k4);
        return;
    }

    ImageBuffer* resultImage = createImageBufferResult();
    if (!resultImage)
        return;
    GraphicsContext* filterContext = resultImage->context();

    FloatRect srcRect = FloatRect(0, 0, -1, -1);
    switch (m_type) {
    case FECOMPOSITE_OPERATOR_OVER:
        filterContext->drawImageBuffer(in2->asImageBuffer(), ColorSpaceDeviceRGB, drawingRegionOfInputImage(in2->absolutePaintRect()));
        filterContext->drawImageBuffer(in->asImageBuffer(), ColorSpaceDeviceRGB, drawingRegionOfInputImage(in->absolutePaintRect()));
        break;
    case FECOMPOSITE_OPERATOR_IN: {
        GraphicsContextStateSaver stateSaver(*filterContext);
        filterContext->clipToImageBuffer(in2->asImageBuffer(), drawingRegionOfInputImage(in2->absolutePaintRect()));
        filterContext->drawImageBuffer(in->asImageBuffer(), ColorSpaceDeviceRGB, drawingRegionOfInputImage(in->absolutePaintRect()));
        break;
    }
    case FECOMPOSITE_OPERATOR_OUT:
        filterContext->drawImageBuffer(in->asImageBuffer(), ColorSpaceDeviceRGB, drawingRegionOfInputImage(in->absolutePaintRect()));
        filterContext->drawImageBuffer(in2->asImageBuffer(), ColorSpaceDeviceRGB, drawingRegionOfInputImage(in2->absolutePaintRect()), srcRect, CompositeDestinationOut);
        break;
    case FECOMPOSITE_OPERATOR_ATOP:
        filterContext->drawImageBuffer(in2->asImageBuffer(), ColorSpaceDeviceRGB, drawingRegionOfInputImage(in2->absolutePaintRect()));
        filterContext->drawImageBuffer(in->asImageBuffer(), ColorSpaceDeviceRGB, drawingRegionOfInputImage(in->absolutePaintRect()), srcRect, CompositeSourceAtop);
        break;
    case FECOMPOSITE_OPERATOR_XOR:
        filterContext->drawImageBuffer(in2->asImageBuffer(), ColorSpaceDeviceRGB, drawingRegionOfInputImage(in2->absolutePaintRect()));
        filterContext->drawImageBuffer(in->asImageBuffer(), ColorSpaceDeviceRGB, drawingRegionOfInputImage(in->absolutePaintRect()), srcRect, CompositeXOR);
        break;
    default:
        break;
    }
}
Exemple #5
0
void FEComposite::apply(Filter* filter)
{
    m_in->apply(filter);
    m_in2->apply(filter);
    if (!m_in->resultImage() || !m_in2->resultImage())
        return;

    GraphicsContext* filterContext = getEffectContext();
    if (!filterContext)
        return;

    FloatRect srcRect = FloatRect(0.f, 0.f, -1.f, -1.f);
    switch (m_type) {
    case FECOMPOSITE_OPERATOR_OVER:
        filterContext->drawImageBuffer(m_in2->resultImage(), DeviceColorSpace, calculateDrawingRect(m_in2->scaledSubRegion()));
        filterContext->drawImageBuffer(m_in->resultImage(), DeviceColorSpace, calculateDrawingRect(m_in->scaledSubRegion()));
        break;
    case FECOMPOSITE_OPERATOR_IN:
        filterContext->save();
        filterContext->clipToImageBuffer(m_in2->resultImage(), calculateDrawingRect(m_in2->scaledSubRegion()));
        filterContext->drawImageBuffer(m_in->resultImage(), DeviceColorSpace, calculateDrawingRect(m_in->scaledSubRegion()));
        filterContext->restore();
        break;
    case FECOMPOSITE_OPERATOR_OUT:
        filterContext->drawImageBuffer(m_in->resultImage(), DeviceColorSpace, calculateDrawingRect(m_in->scaledSubRegion()));
        filterContext->drawImageBuffer(m_in2->resultImage(), DeviceColorSpace, calculateDrawingRect(m_in2->scaledSubRegion()), srcRect, CompositeDestinationOut);
        break;
    case FECOMPOSITE_OPERATOR_ATOP:
        filterContext->drawImageBuffer(m_in2->resultImage(), DeviceColorSpace, calculateDrawingRect(m_in2->scaledSubRegion()));
        filterContext->drawImageBuffer(m_in->resultImage(), DeviceColorSpace, calculateDrawingRect(m_in->scaledSubRegion()), srcRect, CompositeSourceAtop);
        break;
    case FECOMPOSITE_OPERATOR_XOR:
        filterContext->drawImageBuffer(m_in2->resultImage(), DeviceColorSpace, calculateDrawingRect(m_in2->scaledSubRegion()));
        filterContext->drawImageBuffer(m_in->resultImage(), DeviceColorSpace, calculateDrawingRect(m_in->scaledSubRegion()), srcRect, CompositeXOR);
        break;
    case FECOMPOSITE_OPERATOR_ARITHMETIC: {
        IntRect effectADrawingRect = calculateDrawingIntRect(m_in->scaledSubRegion());
        RefPtr<CanvasPixelArray> srcPixelArrayA(m_in->resultImage()->getPremultipliedImageData(effectADrawingRect)->data());

        IntRect effectBDrawingRect = calculateDrawingIntRect(m_in2->scaledSubRegion());
        RefPtr<ImageData> imageData(m_in2->resultImage()->getPremultipliedImageData(effectBDrawingRect));
        CanvasPixelArray* srcPixelArrayB(imageData->data());

        arithmetic(srcPixelArrayA, srcPixelArrayB, m_k1, m_k2, m_k3, m_k4);
        resultImage()->putPremultipliedImageData(imageData.get(), IntRect(IntPoint(), resultImage()->size()), IntPoint());
        }
        break;
    default:
        break;
    }
}
int * find_sequences(int *arr, int len)
{
	int *res;
	int i = 0, d1, d2, diff,k=0,r=0;
	res = (int *)malloc(6 * sizeof(int));

	/*for (i = 1; i < len; i++)
	{
	//d1 = arr[len] - arr[len - 1];
	if (arr[i] - arr[i - 1] == arr[i + 1] - arr[i])
	{
	diff = arr[i] - arr[i - 1];
	//as(arr, len);
	}
	break;
	}*/
	if (len < 0 || arr == '\0')
		return NULL;
	else
	{
		while (i < 4)
		{
			if (arr[k + 1] - arr[k] == arr[k + 2] - arr[k + 1])
			{
				res[r] = k;
				r++;
				diff = arr[k + 1] - arr[k];
				k = arithmetic(k, diff, arr, len);
				res[r] = k;
				r++;
			}
			else if (((float)arr[k + 1] / (float)arr[k]) == ((float)arr[k + 2] / (float)arr[k + 1]))
			{
				res[4] = k;
				diff = arr[k + 1] / arr[k];
				k = geometric(k, diff, arr, len);
				res[5] = k;
			}
			i++;
		}
	}

	return res;



	//Return final array which has 6indexes [AP1_S,AP1_E,AP2_S,AP2_E,GP1_S,GP2_E]
	return NULL;
}
Exemple #7
0
int main(int argc, char **argv)
{
    tod = argc > 1 ? atoi(argv[1]) : 1;
    hires = (argc > 2 ? atoi(argv[2]) : 1) * HR_SECOND;

    normal();

    arithmetic();

    conversion();

    output();

    misc();

    return 0;
}
int main()
{
    printf("...............Start...............\n");

    //open my char device:
    int fd = open("/dev/mydev", O_RDWR);
    if(fd == -1) {
        printf("can't open device!\n");
        return -1;
    }

    int ret;

    ret = 102062527;
    if (ioctl(fd, HW5_IOCSETSTUID, &ret) < 0) {
        printf("set stuid failed\n");
        return -1;
    }

    ret = 1;
    if (ioctl(fd, HW5_IOCSETRWOK, &ret) < 0) {
        printf("set rw failed\n");
        return -1;
    }

    ret = 1;
    if (ioctl(fd, HW5_IOCSETIOCOK, &ret) < 0) {
        printf("set ioc failed\n");
        return -1;
    }

    ret = 1;
    if (ioctl(fd, HW5_IOCSETIRQOK, &ret) < 0) {
        printf("set irq failed\n");
        return -1;
    }

    arithmetic(fd, '+', 100, 10);
    arithmetic(fd, '-', 100, 10);
    arithmetic(fd, '*', 100, 10);
    arithmetic(fd, '/', 100, 10);
    arithmetic(fd, 'p', 100, 10000);
    arithmetic(fd, 'p', 100, 20000);


    printf("...............End...............\n");

    return 0;
}
Exemple #9
0
bool Table::num_comparison( expression * ep, string row )
{
  if ( !ep )
    return false;

  expression* node[2];
  node[0] = ep->values[0].ep;
  node[1] = ep->values[1].ep;
  int v_lhs = 0;
  int v_rhs = 0;
  int ops[2];

  // tokenize row
  vector<string> row_v;
  char *s = toChars( row );
  char *token;

  token = strtok( s, "\t" );
  while ( token )
  {
    row_v.push_back( string( token ) );
    token = strtok ( NULL, "\t" );
  }
  if ( row_v.size() != cols.size() )
    return false;

  // see if nodes are number or string, 
  // if it is string, we need to find the actual value in database
  for ( int i = 0; i < 2; i++ )
  {
    if ( node[i]->func != OP_COLNAME && node[i]->func != OP_NUMBER )
    {
      ops[i] = arithmetic( node[i], row );
    }

    else if ( node[i]->func == OP_NUMBER ) 
    {
      ops[i] = node[i]->values[0].num;
    }
    else if ( node[i]->func == OP_COLNAME )
    {
      int pos = get_col_pos( string( node[i]->values[0].name ) );
      if ( pos < 0 )
      {
        cout << "column does not exist!" << endl;
        return false;
      }
      ops[i] = atoi( row_v[pos].c_str() );
    }
  }

    // start comparison
  switch( ep->func ){

    case OP_EQUAL:  return ops[0] == ops[1]; break;
    case OP_NOTEQ:  return ops[0] != ops[1]; break;
    case OP_LEQ:    return ops[0] <= ops[1]; break;
    case OP_GEQ:    return ops[0] >= ops[1]; break;
    case OP_LT:     return ops[0] < ops[1]; break;
    case OP_GT:     return ops[0] > ops[1]; break;
    default : 
              cout << "OPERATOR NOT SUPPORTED! " << endl;
              return false;
              break;

  }
}
Exemple #10
0
int Table::arithmetic( expression * ep, string row )
{
  if ( !ep )
  {
    cout << "error in Table::arithmetic()" << endl;
    return 0;
  }

  expression* node[2];
  node[0] = ep->values[0].ep;
  node[1] = ep->values[1].ep;
  int v_lhs = 0;
  int v_rhs = 0;
  int ops[2];

  // tokenize row
  vector<string> row_v;
  char *s = toChars( row );
  char *token;

  token = strtok( s, "\t" );
  while ( token )
  {
    row_v.push_back( string( token ) );
    token = strtok ( NULL, "\t" );
  }
  if ( row_v.size() != cols.size() )
    return false;

  // see if nodes are number or string, 
  // if it is string, we need to find the actual value in database
  for ( int i = 0; i < 2; i++ )
  {

    if ( node[i]->func != OP_COLNAME && node[i]->func != OP_NUMBER )
      return arithmetic( node[i], row );
    else if ( node[i]->func == OP_NUMBER ) 
    {
      ops[i] = node[i]->values[0].num;
    }
    else if ( node[i]->func == OP_COLNAME )
    {
      int pos = get_col_pos( node[i]->values[0].data );
      if ( pos < 0 )
      {
        cout << "column does not exist!" << endl;
        return false;
      }
      ops[i] = atoi( row_v[pos].c_str() );
    }
  }

    // start comparison
  switch( ep->func ){

    case OP_PLUS:     return ops[0] + ops[1]; break;
    case OP_BMINUS:   return ops[0] - ops[1]; break;
    case OP_TIMES:    return ops[0] * ops[1]; break;
    case OP_DIVIDE:   return ops[0] / ops[1]; break;
    case OP_UMINUS:   return -ops[0]; break;
    default : 
              cout << "OPERATOR NOT SUPPORTED! " << endl;
              return false;
              break;

  }
}
Exemple #11
0
/*
 +------------------------------------------------------------------
 | FUNCTION  : number_add
 | INPUT     : number : getal
 |             number : getal 
 | OUTPUT    : -
 | RETURN    : een som van 2 getallen
 | DATE      :
 |
 | ABSTRACT  : Optellen van 2 getallen.
 |
 | CHANGES   :
 +------------------------------------------------------------------
 */
DylanObject *number_add( number *n1, number *n2 )
{
   return ( arithmetic ( Add, n1, n2) ) ;
}
Exemple #12
0
/*
 +------------------------------------------------------------------
 | FUNCTION  : number_sub
 | INPUT     : number : een getal
 |             number : een getal
 | OUTPUT    : -
 | RETURN    : een getal
 | DATE      :
 |
 | ABSTRACT  : aftrekken van 2 getallen.
 |
 | CHANGES   :
 +------------------------------------------------------------------
 */
DylanObject *number_sub( number *n1, number *n2 )
{
   return ( arithmetic ( Sub, n1, n2) ) ;
}
Exemple #13
0
rnmatrix<T> rnmatrix<T>::operator *(const data_type rnright) const
{return arithmetic(*this, rnmatrix<T>(rowDim, colDim, rnright),'*');}
Exemple #14
0
rnmatrix<T> rnmatrix<T>::dotTime(const rnmatrix& rnleft, const rnmatrix& rnright)
{
	return arithmetic(rnleft, rnright,'*');
}
Exemple #15
0
/*
 +------------------------------------------------------------------
 | FUNCTION  : number_mul
 | INPUT     : number : een getal
 |             number : een getal
 | OUTPUT    : -
 | RETURN    : een getal
 | DATE      :
 |
 | ABSTRACT  : Produkt van 2 getallen
 |
 | CHANGES   :
 +------------------------------------------------------------------
 */
DylanObject *number_mul( number *n1, number *n2 )
{
   return ( arithmetic ( Mul, n1, n2) ) ;
}
Exemple #16
0
rnmatrix<T> rnmatrix<T>::operator -(const rnmatrix<T>& rnright) const
{return arithmetic(*this, rnright, '-');}
Exemple #17
0
/*
 *  The main code-generation routine.
 *  f is the stream the code should be written to.
 *  p is a pointer to a doubly linked list of ICs
 *  containing the function body to generate code for.
 *  v is a pointer to the function.
 *  offset is the size of the stackframe the function
 *  needs for local variables.
 */
void gen_code(FILE *f,struct IC *p,struct Var *v,zmax offset){
    #ifdef DEBUG_MARK
    printf("Called gen_code(FILE *f,struct IC *p,struct Var *v,zmax offset)\n");
    printf("\tIdentifier: %s", v->identifier);
    #endif

    //emit function head
    if(v->storage_class==EXTERN){
        if( (v->flags & (INLINEFUNC|INLINEEXT)) != INLINEFUNC ){
            emit(f,".EXPORT \t %s \n",v->identifier);
        }
        emit(f,"%s: \n",v->identifier);
    }
    else{
        emit(f,"L_%ld:\n",zm2l(v->offset));
    }

    //emit function prologue
    emit(f, "\tPUSH \t %s\n", regnames[FP]);  //push FP
    emit(f, "\tOR   \t %s %s %s\n",regnames[R0], regnames[SP], regnames[FP]); //MOVE SP -> FP

    //make space for auto variables at stack
    for(int i = 0; i < zm2l(offset); i++){
        emit(f, "\tDEC \t %s %s\n", regnames[SP], regnames[SP]);
    }

    //store backend registers
    emit(f, "\tPUSH \t R1\n\tPUSH \t R2\n\tPUSH \t R3\n\tPUSH \t R4\n");

    //find used registers
    int saved_regs[7] = {0, 0, 0, 0, 0, 0, 0};

    struct IC *ps = p;
    for(;ps;ps=ps->next){
        if( ((ps->code) != FREEREG) && ((ps->code) != ALLOCREG) ){
            if(((ps->q1.flags) & (KONST|VAR|REG|DREFOBJ|VARADR)) == REG){
                if(((ps->q1.reg) > R5) && ((ps->q1.reg) < FP)){
                    saved_regs[(ps->q1.reg) - 7] = 1;
                }
            }
            if(((ps->q2.flags) & (KONST|VAR|REG|DREFOBJ|VARADR)) == REG){
                if(((ps->q2.reg) > R5) && ((ps->q2.reg) < FP)){
                    saved_regs[(ps->q2.reg) - 7] = 1;
                }
            }
            if(((ps->z.flags) & (KONST|VAR|REG|DREFOBJ|VARADR)) == REG){
                if(((ps->z.reg) > R5) && ((ps->z.reg) < FP)){
                    saved_regs[(ps->z.reg) - 7] = 1;
                }
            }
        }
    }

    //save used registers
    for(int i = 0; i < 7; i++){
        if(saved_regs[i] == 1){
            emit(f, "\tPUSH \t %s\n", regnames[i + 7]);
        }
    }

    //emit function body
    for(;p;p=p->next){
        int c = p->code;

        #ifdef DEBUG_MARK
        emit(f, "\n\t;p->code: %d\n", p->code);
        #endif

        switch(p->code){
            case ASSIGN:
                #ifdef DEBUG_MARK
                printf("\n\tASSIGN\n\tz.flags:%d\tq1.flags:%d\ttypf:%d\n", p->z.flags, p->q1.flags, p->typf);
                #endif

                //we can simplify assign when both operands are in registers
                if((((p->q1.flags) & (KONST|VAR|REG|DREFOBJ|VARADR)) == REG) &&
                   (((p->z.flags) & (KONST|VAR|REG|DREFOBJ|VARADR)) == REG) ){
                    emit(f, "\tOR   \t %s %s %s",regnames[R0], regnames[p->q1.reg], regnames[p->z.reg]);
                }

                //this is another optimalization, if have to assign zero; then
                //zero is read from R0 insted pushing constant into register
                else if(
                (((p->q1.flags) & (KONST|VAR|REG|DREFOBJ|VARADR)) == KONST) &&
                ((p->q1.val.vmax) == 0)
                ){
                    store_from_reg(f, R0, &(p->z), p->typf, R2, R3);
                }

                else{
                    load_into_reg(f, R1, &(p->q1), p->typf, R2);
                    store_from_reg(f, R1, &(p->z), p->typf, R2, R3);
                }

                break;
            case OR:
                #ifdef DEBUG_MARK
                printf("\n\tOR\n");
                #endif
                arithmetic(f, p);
                break;
            case XOR:
                #ifdef DEBUG_MARK
                printf("\n\tXOR\n");
                #endif
                arithmetic(f, p);
                break;
            case AND:
                #ifdef DEBUG_MARK
                printf("\n\tAND\n");
                #endif
                arithmetic(f, p);
                break;
            case LSHIFT:
                #ifdef DEBUG_MARK
                printf("\n\tLSHIFT\n");
                #endif
                arithmetic(f, p);
                break;
            case RSHIFT:
                #ifdef DEBUG_MARK
                printf("\n\tRSHIFT\n");
                #endif
                arithmetic(f, p);
                break;
            case ADD:
                #ifdef DEBUG_MARK
                printf("\n\tADD\n");
                #endif
                arithmetic(f, p);
                break;
            case SUB:
                #ifdef DEBUG_MARK
                printf("\n\tSUB\n");
                #endif
                arithmetic(f, p);
                break;
            case MULT:
                #ifdef DEBUG_MARK
                printf("\n\tMULT\n");
                #endif
                arithmetic(f, p);
                break;
            case DIV:
                #ifdef DEBUG_MARK
                printf("\n\tDIV\n");
                #endif
                arithmetic(f, p);
                break;
            case MOD:
                #ifdef DEBUG_MARK
                printf("\n\tMOD\n");
                #endif
                arithmetic(f, p);
                break;
            case KOMPLEMENT:
                #ifdef DEBUG_MARK
                printf("\n\tKOMPLEMENT\n");
                #endif
                arithmetic(f, p);
                break;
            case MINUS:
                #ifdef DEBUG_MARK
                printf("\n\tMINUS\n");
                #endif
                arithmetic(f, p);
                break;
            case ADDRESS:
                #ifdef DEBUG_MARK
                printf("\n\tADDRESS\n");
                #endif

                if(
                (((p->q1.flags) & (KONST|VAR|REG|DREFOBJ|VARADR)) == VAR) &&
                (((p->q1.v->storage_class) & (AUTO|REGISTER|STATIC|EXTERN)) == AUTO)
                ){
                    if(ISARRAY(p->q1.v->flags)){
                        load_cons(f, R1, zm2l(p->q1.v->offset)+zm2l(p->q1.val.vmax));
                    }
                    else{
                        load_cons(f, R1, zm2l(p->q1.v->offset));
                    }
                    emit(f, "\tADD \t R1 R13 R1\n");
                    store_from_reg(f, R1, &(p->z), p->typf, R2, R3);
                }
                else{
                    ierror(0);
                }

                break;
            case CALL:
                #ifdef DEBUG_MARK
                printf("\n\tCALL\n\tq1.flags: %d\n", p->q1.flags);
                #endif

                if((p->q1.flags & (VAR|DREFOBJ)) == VAR && p->q1.v->fi && p->q1.v->fi->inline_asm){
                    emit_inline_asm(f,p->q1.v->fi->inline_asm);
                }
                else{

                    if(((p->q1.flags) & (KONST|VAR|REG|DREFOBJ|VARADR)) == VAR){

                        #ifdef DEBUG_MARK
                        printf("\tq1.v->storage_class: %d\n", p->q1.v->storage_class);
                        #endif

                        switch((p->q1.v->storage_class) & (AUTO|REGISTER|STATIC|EXTERN)){
                            case EXTERN:
                                emit(f, "\tCALL \t %s\n", p->q1.v->identifier);
                                for(int i = 0; i < (p->q2.val.vmax); i++){
                                    emit(f, "\tINC \t %s %s\n", regnames[SP], regnames[SP]);
                                }
                                break;
                            case STATIC:
                                emit(f, "\tCALL \t L_%ld\n", zm2l(p->q1.v->offset));
                                for(int i = 0; i < (p->q2.val.vmax); i++){
                                    emit(f, "\tINC \t %s %s\n", regnames[SP], regnames[SP]);
                                }
                                break;
                            default:
                                #ifdef DEBUG_MARK
                                printf("\tThis is not implemented!\n");
                                #else
                                ierror(0);
                                #endif
                                break;
                        }

                    }
                    else if(((p->q1.flags) & (KONST|VAR|REG|DREFOBJ|VARADR)) == (VAR|DREFOBJ)){
                        #ifdef DEBUG_MARK
                        printf("\tq1.v->storage_class: %d\n", p->q1.v->storage_class);
                        #endif                        
                        load_into_reg(f, R1, &(p->q1), p->typf, R3);
                        emit(f, "\tCALLI\t %s\n", regnames[R1]);                        
                        emit(f, "\tINC \t %s %s\n", regnames[SP], regnames[SP]);                        
                    }
                    else{
                        #ifdef DEBUG_MARK
                        printf("\tThis is not implemented!\n");
                        #else
                        ierror(0);
                        #endif
                    }
                }
                break;
            case CONVERT:
                #ifdef DEBUG_MARK
                printf("\n\tCONVERT\n");
                #endif

                break;
            case ALLOCREG:
                #ifdef DEBUG_MARK
                printf("\n\tALLOCREG\n");
                #endif

                regs[p->q1.reg] = 1;
                break;
            case FREEREG:
                #ifdef DEBUG_MARK
                printf("\n\tFREEREG\n");
                #endif

                regs[p->q1.reg] = 0;
                break;
            case COMPARE:
                #ifdef DEBUG_MARK
                printf("\n\tCOMPARE\n");
                #endif

                compare(f, p);
                break;
            case TEST:
                #ifdef DEBUG_MARK
                printf("\n\tTEST\n");
                #endif

                compare(f, p);
                break;
            case LABEL:
                #ifdef DEBUG_MARK
                printf("\n\tLABEL\n");
                #endif

                emit(f,"L_%d:\n",p->typf);
                break;
            case BEQ:
                #ifdef DEBUG_MARK
                printf("\n\tBEQ\n");
                #endif

                emit(f, "\tBNZ \t R4 L_%d\n", p->typf);
                break;
            case BNE:
                #ifdef DEBUG_MARK
                printf("\n\tBNE\n");
                #endif

                emit(f, "\tBNZ \t R4 L_%d\n", p->typf);
                break;
            case BLT:
                #ifdef DEBUG_MARK
                printf("\n\tBLT\n");
                #endif

                emit(f, "\tBNZ \t R4 L_%d\n", p->typf);
                break;
            case BGE:
                #ifdef DEBUG_MARK
                printf("\n\tBGE\n");
                #endif

                emit(f, "\tBNZ \t R4 L_%d\n", p->typf);
                break;
            case BLE:
                #ifdef DEBUG_MARK
                printf("\n\tBLE\n");
                #endif

                emit(f, "\tBNZ \t R4 L_%d\n", p->typf);
                break;
            case BGT:
                #ifdef DEBUG_MARK
                printf("\n\tBGT\n");
                #endif

                emit(f, "\tBNZ \t R4 L_%d\n", p->typf);
                break;
            case BRA:
                #ifdef DEBUG_MARK
                printf("\n\tBRA\n");
                #endif

                emit(f, "\tBZ   \t R0 L_%d\n", p->typf);
                break;
            case PUSH:
                #ifdef DEBUG_MARK
                printf("\n\tPUSH\n");
                #endif

                load_into_reg(f, R1, &(p->q1), p->typf, R2);
                emit(f, "\tPUSH \t R1\n");
                break;
            case ADDI2P:
                #ifdef DEBUG_MARK
                printf("\n\tADDI2P\n");
                #endif
                arithmetic(f, p);
                break;
            case SUBIFP:
                #ifdef DEBUG_MARK
                printf("\n\tSUBIFP\n");
                #endif
                arithmetic(f, p);
                break;
            case SUBPFP:
                #ifdef DEBUG_MARK
                printf("\n\tSUBPFP\n");
                #endif
                arithmetic(f, p);
                break;
            case GETRETURN:
                #ifdef DEBUG_MARK
                printf("\n\tGETRETURN\n");
                #endif
                if((p->q1.reg) != 0){
                    store_from_reg(f, p->q1.reg, &(p->z), p->typf, R2, R3);
                }
                else{
                    #ifdef DEBUG_MARK
                    printf("\tq1.reg == 0, didn't know how to dealt with it!");
                    #else
                    ierror(0);
                    #endif
                }
                break;
            case SETRETURN:
                #ifdef DEBUG_MARK
                printf("\n\tSETRETURN\n\tz.flags:%d\n", p->z.flags);
                #endif
                if((p->z.reg) != 0){
                    load_into_reg(f, p->z.reg, &(p->q1), p->typf, R1);
                }
                else{
                    #ifdef DEBUG_MARK
                    printf("\tz.reg == 0, didn't know how to dealt with it!");
                    #else
                    ierror(0);
                    #endif
                }
                break;
            case MOVEFROMREG:
                #ifdef DEBUG_MARK
                printf("\n\tMOVEFROMREG\n");
                #endif

                store_from_reg(f, p->q1.reg, &(p->z), p->typf, R1, R3);
                break;
            case MOVETOREG:
                #ifdef DEBUG_MARK
                printf("\n\tMOVETOREG\n");
                #endif

                load_into_reg(f, p->z.reg, &(p->q1), p->typf, R1);
                break;
            case NOP:
                #ifdef DEBUG_MARK
                printf("\n\tNOP\n");
                #endif
                break;
            default:
                #ifdef DEBUG_MARK
                printf("\tSomething is wrong in gencode()!\n");
                #else
                ierror(0);
                #endif
                break;
        }
    }

    //restore used registers
    for(int i = 6; i >= 0; i--){
        if(saved_regs[i] == 1){
            emit(f, "\tPOP \t %s\n", regnames[i + 7]);
        }
    }

    //restore backend registers
    emit(f, "\tPOP \t R4\n\tPOP \t R3\n\tPOP \t R2\n\tPOP \t R1\n");

    //emit function epilogue
    emit(f, "\tOR  \t %s %s %s\n",regnames[R0], regnames[FP], regnames[SP]); //restore SP from FP
    emit(f, "\tPOP \t %s\n", regnames[FP]); //restore old FP from stack

    //return
    if((v->tattr)&INTERRUPT){
        emit(f, "\tRETI\n");
    }
    else{
        emit(f, "\tRET\n");
    }
}
Exemple #18
0
/*
 +------------------------------------------------------------------
 | FUNCTION  : number_div
 | INPUT     : number : een getal
 |             number : een getal
 | OUTPUT    : -
 | RETURN    : een getal
 | DATE      :
 |
 | ABSTRACT  : Deling van 2 getallen
 |
 | CHANGES   :
 +------------------------------------------------------------------
 */
DylanObject *number_div( number *n1, number *n2 )
{
   return ( arithmetic ( Div, n1, n2) ) ;
}