[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Tinycc-devel] C language modification using TinyCC - problems
From: |
James Dunne |
Subject: |
[Tinycc-devel] C language modification using TinyCC - problems |
Date: |
Thu, 19 May 2005 23:58:07 -0500 |
Fabrice,
I'd just like to say thank you for TinyCC - I've had much pleasure
playing around with it so far!
I wanted to make a few simple changes to the C language just for fun,
but I came across some problems that I can't figure out:
Use the binary '~' operator for char * concatenations. All I want it
to do is to check for two char *s on either side and return the value
called by:
char *__concat(char *a, char *b);
I've gotten the '~' operator to co-exist as both a binary and unary
operator - named TOK_CAT, and TOK_A_CAT for ~= operations. I've added
the operator to the expr_shift() function and have defined TOK_CAT and
TOK_A_CAT correctly. My problem becomes the function call portion.
In gen_op() I've modified the "if (bt1 == VT_PTR && bt2 == VT_PTR)"
block to allow for "if (op == TOK_CAT)" inside it so that I can
implement it there. I've defined the operator to only work on two
(char *)s. This is the relative block of code that I have "somewhat"
working now:
/* if both pointers, then it must be the '-' op */
if (bt1 == VT_PTR && bt2 == VT_PTR) {
/* get the pointed-to types */
pt1 = pointed_type(&vtop[-1].type)->t;
pt2 = pointed_type(&vtop[ 0].type)->t;
pbt1 = pt1 & VT_BTYPE;
pbt2 = pt2 & VT_BTYPE;
/* string concatenation operator */
if (op == TOK_CAT) {
/* generate function call */
vpush_global_sym(&func_old_type, TOK___concat);
vrott(3);
gfunc_call(2);
/* return value */
vpushi(0);
vtop->r = RC_IRET;
} else if (op == '-') {
My problem is the return value of the function call. The parameters
passed in to my function work correctly, but I cannot get the correct
return value to be used. Here's my sample program:
char *__concat(char *a, char *b) {
char *result = (char *)malloc(strlen(a) + strlen(b) + 1);
printf("%s\n", a);
printf("%s\n", b);
strcpy(result, a);
strcat(result, b);
printf("%s\n", result);
return result;
}
int main(int argc, char **argv) {
char *s1 = "hi ";
char *s2 = "neat!";
char *s3 = "poo";
printf("s1 %p\n", s1);
printf("s2 %p\n", s2);
printf("s3 %p\n", s3);
s3 = s1 ~ s2; // line 20
printf("s1 %p\n", s1);
printf("s2 %p\n", s2);
printf("s3 %p\n", s3);
return 0;
}
My outputs are the following:
ex6.c:20: warning: assignment makes pointer from integer without a cast
s1 0x807eb6c
s2 0x807eb70
s3 0x807eb76
hi
neat!
hi neat!
s1 0x807eb6c
s2 0x807eb70
s3 0xbffff5cc
What am I doing wrong? s3's value looks wrong and produces the same
garbage string each time the program runs. As you can see, __concat
is getting the parameters correct and successfully concatenates them.
The generated code, however, does not process the return type
correctly. I hope it's a small, easy fix! =)
--
Regards,
James Dunne
- [Tinycc-devel] C language modification using TinyCC - problems,
James Dunne <=