aboutsummaryrefslogtreecommitdiffstats
path: root/c_compiler/src/expression.cpp
blob: 2dc3a54523b0375d50e96775d8629e1e8154885c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
#include "expression.hpp"

#include <cstdio>
#include <exception>
#include <vector>

// Expression definition

int Expression::constantFold() const
{
    throw std::runtime_error("Error : Cannot constant fold this expression");
}

void Expression::print() const
{
    if(next_expression_ != nullptr)
	next_expression_->print();

    printf("Expression\n");
}

void Expression::printXml() const
{}

void Expression::countArguments(int &) const
{
    // by default don't do anything to the count
}

void Expression::expressionDepth(int &depth_count) const
{
    if(next_expression_ != nullptr)
	next_expression_->expressionDepth(depth_count);
}

std::string Expression::id() const
{
    // by default return empty id, which cannot be valid.
    return "";
}

TypePtr Expression::getType(const VariableStackBindings &) const
{
    // by default return largest size, which is 32 bits
    return std::make_shared<Int>();
}

void Expression::linkExpression(Expression *next_expression)
{
    ExpressionPtr expression_ptr(next_expression);
    next_expression_ = expression_ptr;
}

ExpressionPtr Expression::nextExpression() const
{
    return next_expression_;
}


// OperationExpression definition

OperationExpression::OperationExpression(Expression *lhs, Expression *rhs)
    : lhs_(lhs), rhs_(rhs)
{}

OperationExpression::OperationExpression(ExpressionPtr lhs, Expression *rhs)
    : lhs_(lhs), rhs_(rhs)
{}

int OperationExpression::constantFold() const
{
    throw std::runtime_error("Error : Cannot constant fold expression");
}

void OperationExpression::expressionDepth(int &depth_count) const
{
    int lhs_depth_count = depth_count;
    int rhs_depth_count = depth_count+1;

    lhs_->expressionDepth(lhs_depth_count);
    rhs_->expressionDepth(rhs_depth_count);

    if(lhs_depth_count > rhs_depth_count)
	depth_count = lhs_depth_count;
    else
	depth_count = rhs_depth_count;
}

TypePtr OperationExpression::getType(const VariableStackBindings &bindings) const
{
    return lhs_->getType(bindings);
}

ExpressionPtr OperationExpression::getLhs() const
{
    return lhs_;
}

ExpressionPtr OperationExpression::getRhs() const
{
    return rhs_;
}

void OperationExpression::evaluateExpression(VariableStackBindings bindings, int &label_count) const
{
    // I can just evaluate the lhs with the same entry stack position
    lhs_->printAsm(bindings, label_count);

    // store this stack position
    int lhs_stack_position = bindings.currentExpressionStackPosition();

    // now have to increase the expression stack position for the rhs
    bindings.nextExpressionStackPosition();
    rhs_->printAsm(bindings, label_count);

    // now I have them evaluated at two positions in the stack and can load both into registers
    // $2 and $3
    lhs_->getType(bindings)->load(2, lhs_stack_position);
    printf("\tlw\t$3,%d($fp)\n", bindings.currentExpressionStackPosition());
}


// Unary expression definition

void UnaryExpression::stackPosition(VariableStackBindings, int &) const
{
    throw std::runtime_error("Error : Cannot get stack position of expression");
}


// PostfixArrayElement

PostfixArrayElement::PostfixArrayElement(Expression *postfix_expression, Expression *index_expression)
    : postfix_expression_(postfix_expression), index_expression_(index_expression)
{}

VariableStackBindings PostfixArrayElement::printAsm(VariableStackBindings bindings, int &label_count) const
{
    stackPosition(bindings, label_count);
    TypePtr type_ptr = postfix_expression_->getType(bindings);
    type_ptr->load();
    printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition());
    return bindings;
}

void PostfixArrayElement::stackPosition(VariableStackBindings bindings, int &label_count) const
{
    index_expression_->printAsm(bindings, label_count);

    std::shared_ptr<UnaryExpression> unary_expression;
    unary_expression = std::static_pointer_cast<UnaryExpression>(postfix_expression_);
    unary_expression->stackPosition(bindings, label_count);

    printf("\tli\t$3,4\n\tmul\t$2,$2,$3\n\taddu\t$t0,$t0,$2\n");
}

void PostfixArrayElement::expressionDepth(int &depth_count) const
{
    if(nextExpression() != nullptr)
	nextExpression()->expressionDepth(depth_count);

    if(index_expression_ != nullptr)
	index_expression_->expressionDepth(depth_count);
}

TypePtr PostfixArrayElement::getType(const VariableStackBindings &bindings) const
{
    return postfix_expression_->getType(bindings);
}


// PostfixFunctionCall

PostfixFunctionCall::PostfixFunctionCall(Expression *argument_expression_list)
    : argument_expression_list_(argument_expression_list)
{}

VariableStackBindings PostfixFunctionCall::printAsm(VariableStackBindings bindings, int &label_count) const
{
    std::vector<ExpressionPtr> argument_vector;
    ExpressionPtr current_argument = argument_expression_list_;
    int argument_counter = 0;

    while(current_argument != nullptr)
    {
	argument_vector.push_back(current_argument);
	current_argument = current_argument->nextExpression();
    }

    for(auto itr = argument_vector.rbegin(); itr != argument_vector.rend(); ++itr)
    {
	(*itr)->printAsm(bindings, label_count);

	if(argument_counter < 4)
	    printf("\tmove\t$%d,$2\n", 4+argument_counter);
	else
	    (*itr)->getType(bindings)->store(4*argument_counter);

	argument_counter++;
    }

    printf("\tjal\t%s\n\tnop\n\tsw\t$2,%d($fp)\n",
	   postfix_expression_->id().c_str(), bindings.currentExpressionStackPosition());
    return bindings;
}

void PostfixFunctionCall::countArguments(int &argument_count) const
{
    ExpressionPtr current_argument = argument_expression_list_;

    argument_count = 0;
    
    while(current_argument != nullptr)
    {
	argument_count++;
	current_argument = current_argument->nextExpression();
    }
}

void PostfixFunctionCall::setPostfixExpression(Expression *postfix_expression)
{
    ExpressionPtr expression_ptr(postfix_expression);
    postfix_expression_ = expression_ptr;
}

void PostfixFunctionCall::expressionDepth(int &depth_count) const
{
    if(argument_expression_list_ != nullptr)
	argument_expression_list_->expressionDepth(depth_count);
}

TypePtr PostfixFunctionCall::getType(const VariableStackBindings &) const
{
    return std::make_shared<Int>();
}


// Post increment and decrement definition

PostfixPostIncDecExpression::PostfixPostIncDecExpression(const std::string &_operator, Expression *postfix_expression)
    : operator_(_operator), postfix_expression_(postfix_expression)
{}

VariableStackBindings PostfixPostIncDecExpression::printAsm(VariableStackBindings bindings, int &label_count) const
{
    postfix_expression_->printAsm(bindings, label_count);
    if(operator_ == "++")
	printf("\taddiu\t$3,$2,1\n");
    else if(operator_ == "--")
	printf("\tsubiu\t$3,$2,1\n");
    else
	throw std::runtime_error("Error : '"+operator_+"' not recognized");

    std::shared_ptr<UnaryExpression> unary_expression;
    unary_expression = std::static_pointer_cast<UnaryExpression>(postfix_expression_);

    printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition());
    unary_expression->stackPosition(bindings, label_count);
    TypePtr tmp_ptr = postfix_expression_->getType(bindings);
    if(std::dynamic_pointer_cast<Char>(tmp_ptr))
    {	
	printf("\tsb\t$3,0($t0)\n");
    }
    else if(std::dynamic_pointer_cast<Short>(tmp_ptr))
    {
	printf("\tsh\t$3,0($t0)\n");
    }
    else
    {
	printf("\tsw\t$3,0($t0)\n");
    }
    
    return bindings;
}

TypePtr PostfixPostIncDecExpression::getType(const VariableStackBindings &bindings) const
{
    return postfix_expression_->getType(bindings);
}


// Pre increment and decrement implementation

UnaryPreIncDecExpression::UnaryPreIncDecExpression(const std::string &_operator, Expression *unary_expression)
    : operator_(_operator), unary_expression_(unary_expression)
{}

VariableStackBindings UnaryPreIncDecExpression::printAsm(VariableStackBindings bindings, int &label_count) const
{
    unary_expression_->printAsm(bindings, label_count);
    if(operator_ == "++")
	printf("\taddi\t$2,$2,1\n");
    else if(operator_ == "--")
	printf("\tsubi\t$2,$2,1\n");
    else
	throw std::runtime_error("Error : '"+operator_+"' not recognized");

    std::shared_ptr<UnaryExpression> unary_expression;
    unary_expression = std::static_pointer_cast<UnaryExpression>(unary_expression_);

    printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition());
    unary_expression->stackPosition(bindings, label_count);
    unary_expression_->getType(bindings)->store();
    return bindings;
}

TypePtr UnaryPreIncDecExpression::getType(const VariableStackBindings &bindings) const
{
    return unary_expression_->getType(bindings);
}


// Operator unary definition

OperatorUnaryExpression::OperatorUnaryExpression(const std::string &_operator, Expression *cast_expression)
    : operator_(_operator), cast_expression_(cast_expression)
{}

VariableStackBindings OperatorUnaryExpression::printAsm(VariableStackBindings bindings, int &label_count) const
{
    cast_expression_->printAsm(bindings, label_count);
    if(operator_ == "!")
    {
	printf("\tsltu\t$2,$2,1\n\tandi\t$2,$2,0x00ff\n");
    }
    else if(operator_ == "~")
    {
	printf("\tnor\t$2,$0,$2\n");
    }
    else if(operator_ == "&")
    {
	std::shared_ptr<UnaryExpression> unary_expression;
	unary_expression = std::static_pointer_cast<UnaryExpression>(cast_expression_);
	unary_expression->stackPosition(bindings, label_count);
	printf("\tmove\t$2,$t0\n");
    }
    else if(operator_ == "-")
    {
	printf("\tsubu\t$2,$0,$2\n");
    }
    else if(operator_ == "*")
    {
	printf("\tlw\t$2,0($2)\n");
    }

    printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition());

    return bindings;
}

void OperatorUnaryExpression::stackPosition(VariableStackBindings bindings, int &label_count) const
{
    if(operator_ == "*")
    {	
	std::shared_ptr<UnaryExpression> unary_expression;
	unary_expression = std::static_pointer_cast<UnaryExpression>(cast_expression_);
	unary_expression->stackPosition(bindings, label_count);
	printf("\tlw\t$t0,0($t0)\n");
    }
}

TypePtr OperatorUnaryExpression::getType(const VariableStackBindings &bindings) const
{
    return cast_expression_->getType(bindings);
}


// CastExpression definition

CastExpression::CastExpression(Type *type, Expression *expression)
    : type_(type), expression_(expression)
{}

VariableStackBindings CastExpression::printAsm(VariableStackBindings bindings, int &label_count) const
{
    return bindings;
}

void CastExpression::expressionDepth(int &depth_count) const
{
    if(nextExpression() != nullptr)
	nextExpression()->expressionDepth(depth_count);

    expression_->expressionDepth(depth_count);
}

TypePtr CastExpression::getType(const VariableStackBindings &) const
{
    return type_;
}


// Additive Expression definition

AdditiveExpression::AdditiveExpression(Expression *lhs, const std::string &_operator, Expression *rhs)
    : OperationExpression(lhs, rhs), operator_(_operator)
{}

VariableStackBindings AdditiveExpression::printAsm(VariableStackBindings bindings, int &label_count) const
{
    evaluateExpression(bindings, label_count);
    
    // TODO currently using signed and sub because I only have signed numbers implemented
    // must update this as I add more types
    if(operator_ == "+")
	printf("\taddu\t$2,$2,$3\n");
    else if(operator_ == "-")
	printf("\tsubu\t$2,$2,$3\n");
    else
	throw std::runtime_error("Error : '"+operator_+"' not recognized");

    // now I have to store it back into the original stack position
    printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition());

    return bindings;
}

int AdditiveExpression::constantFold() const
{
    if(operator_ == "+")
	return lhs_->constantFold()+rhs_->constantFold();
    return lhs_->constantFold()-rhs_->constantFold();
}


// Multiplicative Expression definition


MultiplicativeExpression::MultiplicativeExpression(Expression *lhs, const std::string &_operator, Expression *rhs)
    : OperationExpression(lhs, rhs), operator_(_operator)
{}

VariableStackBindings MultiplicativeExpression::printAsm(VariableStackBindings bindings, int &label_count) const
{
    evaluateExpression(bindings, label_count);

    // then perform the right operation
    if(operator_ == "*")
    {
	printf("\tmul\t$2,$2,$3\n");	
    }
    else if(operator_ == "/" || operator_ == "%")
    {
	printf("\tdiv\t$2,$3\n");
	if(operator_ == "/")
	    printf("\tmflo\t$2\n");
	else
	    printf("\tmfhi\t$2\n");	
    }
    else
    {
	throw std::runtime_error("Error : '"+operator_+"' not recognized");
    }
	

    // finally store result back into the stack position
    printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition());

    return bindings;
}

int MultiplicativeExpression::constantFold() const
{
    if(operator_ == "*")
	return lhs_->constantFold()*rhs_->constantFold();
    else if(operator_ == "/")
	return lhs_->constantFold()/rhs_->constantFold();
    return lhs_->constantFold()%rhs_->constantFold();
}


// ShiftExpression definition

ShiftExpression::ShiftExpression(Expression* lhs, const std::string &_operator, Expression *rhs)
    : OperationExpression(lhs, rhs), operator_(_operator)
{}

VariableStackBindings ShiftExpression::printAsm(VariableStackBindings bindings, int &label_count) const
{
    evaluateExpression(bindings, label_count);

    if(operator_ == "<<")
    {
	printf("\tsll\t$2,$2,$3\n");
    }
    else if(operator_ == ">>")
    {
	printf("\tsra\t$2,$2,$3\n");
    }
    else
    {
	throw std::runtime_error("Error : '"+operator_+"' not recognized");
    }

    printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition());

    return bindings;
}

int ShiftExpression::constantFold() const
{
    if(operator_ == "<<")
	return lhs_->constantFold()<<rhs_->constantFold();
    return lhs_->constantFold()>>rhs_->constantFold();
}


// RelationalExpression definition

RelationalExpression::RelationalExpression(Expression* lhs, const std::string &_operator, Expression *rhs)
    : OperationExpression(lhs, rhs), operator_(_operator)
{}

VariableStackBindings RelationalExpression::printAsm(VariableStackBindings bindings, int &label_count) const
{
    evaluateExpression(bindings, label_count);

    if(operator_ == "<")
    {
	printf("\tslt\t$2,$2,$3\n");
    }
    else if(operator_ == ">")
    {
	printf("\tslt\t$2,$3,$2\n");
    }
    else if(operator_ == "<=")
    {
	printf("\tslt\t$2,$3,$2\n\txori\t$2,$2,0x1\n");
    }
    else if(operator_ == ">=")
    {
	printf("\tslt\t$2,$2,$3\n\txori\t$2,$2,0x1\n");
    }
    else
    {
	throw std::runtime_error("Error : '"+operator_+"' not recognized");
    }

    // TODO might get rid of this
    printf("\tandi\t$2,$2,0x00ff\n\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition());
    return bindings;
}

int RelationalExpression::constantFold() const
{
    if(operator_ == "<")
    {
	return lhs_->constantFold()<rhs_->constantFold();
    }
    else if(operator_ == ">")
    {
	return lhs_->constantFold()>rhs_->constantFold();
    }
    else if(operator_ == "<=")
    {
	return lhs_->constantFold()<=rhs_->constantFold();
    }
    
    return lhs_->constantFold()>=rhs_->constantFold();
}


// EqualityExpression definition

EqualityExpression::EqualityExpression(Expression *lhs, const std::string &_operator, Expression *rhs)
    : OperationExpression(lhs, rhs), operator_(_operator)
{}

VariableStackBindings EqualityExpression::printAsm(VariableStackBindings bindings, int &label_count) const
{
    evaluateExpression(bindings, label_count);
    printf("\txor\t$2,$2,$3\n");

    if(operator_ == "==")
    {
	printf("\tsltiu\t$2,$2,1\n");
    }
    else if(operator_ == "!=")
    {
	printf("\tsltu\t$2,$0,$2\n");
    }
    else
    {
	throw std::runtime_error("Error : '"+operator_+"' not recognized");
    }

    // TODO Work out why it is necessary to remove bytes 3 and 2.
    printf("\tandi\t$2,$2,0x00ff\n\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition());
    return bindings;
}

int EqualityExpression::constantFold() const
{
    if(operator_ == "==")
	return lhs_->constantFold()==rhs_->constantFold();
    return lhs_->constantFold()!=rhs_->constantFold();
}


// AndExpression definition

AndExpression::AndExpression(Expression *lhs, Expression *rhs)
    : OperationExpression(lhs, rhs)
{}

VariableStackBindings AndExpression::printAsm(VariableStackBindings bindings, int &label_count) const
{
    evaluateExpression(bindings, label_count);
    printf("\tand\t$2,$2,$3\n\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition());
    return bindings;
}

int AndExpression::constantFold() const
{
    return lhs_->constantFold()&rhs_->constantFold();
}


// ExclusiveOrExpression definition

ExclusiveOrExpression::ExclusiveOrExpression(Expression *lhs, Expression *rhs)
    : OperationExpression(lhs, rhs)
{}

VariableStackBindings ExclusiveOrExpression::printAsm(VariableStackBindings bindings, int &label_count) const
{
    evaluateExpression(bindings, label_count);
    printf("\txor\t$2,$2,$3\n\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition());
    return bindings;
}

int ExclusiveOrExpression::constantFold() const
{
    return lhs_->constantFold()^rhs_->constantFold();
}


// InclusiveOrExpression definition

InclusiveOrExpression::InclusiveOrExpression(Expression *lhs, Expression *rhs)
    : OperationExpression(lhs, rhs)
{}

VariableStackBindings InclusiveOrExpression::printAsm(VariableStackBindings bindings, int &label_count) const
{
    evaluateExpression(bindings, label_count);
    printf("\tor\t$2,$2,$3\n\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition());
    return bindings;
}

int InclusiveOrExpression::constantFold() const
{
    return lhs_->constantFold()|rhs_->constantFold();
}


// LogicalAndExpression definition

LogicalAndExpression::LogicalAndExpression(Expression *lhs, Expression *rhs)
    : OperationExpression(lhs, rhs)
{}

VariableStackBindings LogicalAndExpression::printAsm(VariableStackBindings bindings, int &label_count) const
{
    int log_and = label_count++;
    lhs_->printAsm(bindings, label_count);
    printf("\tbeq\t$2,$0,$%d_log_and_load_0\n\tnop\n", log_and);
    rhs_->printAsm(bindings, label_count);
    printf("\tbeq\t$2,$0,$%d_log_and_load_0\n\tnop\n", log_and);
    printf("\tli\t$2,1\n\tb\t$%d_log_and_end\n\tnop\n", log_and);
    printf("$%d_log_and_load_0:\n\tmove\t$2,$0\n$%d_log_and_end:\n", log_and, log_and);
    printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition());
    return bindings;
}

int LogicalAndExpression::constantFold() const
{
    return lhs_->constantFold()&&rhs_->constantFold();
}


// LogicalOrExpression definition

LogicalOrExpression::LogicalOrExpression(Expression *lhs, Expression *rhs)
    : OperationExpression(lhs, rhs)
{}

VariableStackBindings LogicalOrExpression::printAsm(VariableStackBindings bindings, int &label_count) const
{
    int log_or = label_count++;
    lhs_->printAsm(bindings, label_count);
    printf("\tbne\t$2,$0,$%d_log_or_load_1\n\tnop\n", log_or);
    rhs_->printAsm(bindings, label_count);
    printf("\tbeq\t$2,$0,$%d_log_or_load_0\n\tnop\n", log_or);
    printf("$%d_log_or_load_1:\n\tli\t$2,1\n\tb\t$%d_log_or_end\n\tnop\n", log_or, log_or);
    printf("$%d_log_or_load_0:\n\tmove\t$2,$0\n$%d_log_or_end:\n", log_or, log_or);
    printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition());
    return bindings;
}

int LogicalOrExpression::constantFold() const
{
    return lhs_->constantFold()&&rhs_->constantFold();
}


// ConditionalExpression definition

ConditionalExpression::ConditionalExpression(Expression *logical_or,
					     Expression *expression,
					     Expression *conditional_expression)
    : logical_or_(logical_or), expression_(expression),
      conditional_expression_(conditional_expression)
{}

VariableStackBindings ConditionalExpression::printAsm(VariableStackBindings bindings, int &label_count) const
{
    return bindings;
}

TypePtr ConditionalExpression::getType(const VariableStackBindings &bindings) const
{
    return std::make_shared<Int>();
}


// Assignment Expression definition

AssignmentExpression::AssignmentExpression(Expression *lhs, Expression *rhs)
    : OperationExpression(lhs, rhs)
{}

AssignmentExpression::AssignmentExpression(ExpressionPtr lhs, Expression *rhs)
    : OperationExpression(lhs, rhs)
{}

VariableStackBindings AssignmentExpression::printAsm(VariableStackBindings bindings, int &label_count) const
{
    // TODO add stack and store results in there, also for addition and multiplication.

    // get the current location of lhs in the stack so that I can store result there
    std::shared_ptr<UnaryExpression> lhs_postfix;
    lhs_postfix = std::static_pointer_cast<UnaryExpression>(lhs_);

    // get the current available stack position
    int expression_stack_position = bindings.currentExpressionStackPosition();

    // evaluate rhs and get the result back at the stack position I assigned
    // don't have to change the stack position as there is no lhs to evaluate
    rhs_->printAsm(bindings, label_count);
    bindings.nextExpressionStackPosition();

    // we are assigning so we don't have to evaluate the lhs as it will be overwritten anyways
    lhs_postfix->stackPosition(bindings, label_count);
    
    // now the result of the rhs will be in that stack position, so we can load it into $2
    TypePtr tmp_ptr = lhs_->getType(bindings);
    tmp_ptr->load(2, expression_stack_position);
    tmp_ptr->store();
    return bindings;
}


// Identifier definition

Identifier::Identifier(const std::string &id)
    : id_(id)
{}

VariableStackBindings Identifier::printAsm(VariableStackBindings bindings, int &) const
{
    if(bindings.bindingExists(id_))
    {
	int stack_position = bindings.stackPosition(id_);
	if(stack_position == -1)
	{
	    // it's a global variable
	    printf("\tlui\t$2,%%hi(%s)\n\tlw\t$2,%%lo(%s)($2)\n", id_.c_str(), id_.c_str());   
	}
	else
	{
	    if(std::dynamic_pointer_cast<Array>(bindings.getType(id_)) != nullptr)
	    {
		printf("\taddiu\t$2,$fp,%d\n", stack_position);
	    }
	    else
	    {
		bindings.getType(id_)->load(2, stack_position);
	    }
	}
    }
    else
    {
	throw std::runtime_error("Error : Can't find '"+id_+"' in current scope binding");
    }

    printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition());
    return bindings;
}

void Identifier::stackPosition(VariableStackBindings bindings, int &) const
{
    if(bindings.bindingExists(id_))
    {
	printf("\taddiu\t$t0,$fp,%d\n", bindings.stackPosition(id_));
	return;
    }

    throw std::runtime_error("Error : '"+id_+"' not yet declared");
}

std::string Identifier::id() const
{
    return id_;
}

TypePtr Identifier::getType(const VariableStackBindings &bindings) const
{
    return bindings.getType(id_);
}


// Constant definition

Constant::Constant(const int32_t &constant)
    : constant_(constant)
{}

VariableStackBindings Constant::printAsm(VariableStackBindings bindings, int &) const
{
    // constant only has to load to $2 because the other expression will take care of the rest
    printf("\tli\t$2,%d\n", constant_);
    printf("\tsw\t$2,%d($fp)\n", bindings.currentExpressionStackPosition());
    return bindings;
}

int Constant::constantFold() const
{
    return constant_;
}

TypePtr Constant::getType(const VariableStackBindings &) const
{
    return std::make_shared<Int>();
}