shithub: scc

ref: 7c88dc7a0c96e24b53b0aeea66d74b3a757be2d4
dir: /cc2/optm.c/

View raw version

#include <stddef.h>
#include <stdint.h>

#include <cc.h>
#include "cc2.h"


#include <stdio.h>

static Node *
optcasts(Node *np, Type *tp)
{
	if (!np)
		return NULL;

repeat:
	switch (np->op) {
	case OCAST:
		/* TODO: be careful with the sign */
		if (np->type->c_int && np->type->size >= tp->size) {
			np = np->left;
			goto repeat;
		}
		break;
	case OASSIG:
		tp = np->type;
		break;
	default:
		np->type = tp;
	}

	np->left = optcasts(np->left, tp);
	np->right = optcasts(np->right, tp);
	return np;			
}

Node *
optimize(Node *np)
{
	np = optcasts(np, np->type);
	return np;
}