December 16, 2003
Roberto Mariottini wrote:
> I realized that maybe an ordinary function like checkoverflow can be already
> done with inline assembly. Is it true?

No, since you cannot rely upon execution order, and the compiler is free to insert any stuff between the statements it likes, which is very likely to zero the flags.

The syntax must thus imply which operation *exactly* has to be checked.

I would guess something like

int bla = overloadguard(expr);

bla gets assigned the result of expression, and the outermost operation is checked in expr against overflow. Or maybe only allow expr to be an atomic expression, do not allow nesting.

-eye

December 16, 2003
Ilya Minkov wrote:

> Roberto Mariottini wrote:
>
>> I realized that maybe an ordinary function like checkoverflow can be already
>> done with inline assembly. Is it true?
>
>
> No, since you cannot rely upon execution order, and the compiler is free to insert any stuff between the statements it likes, which is very likely to zero the flags.
>
> The syntax must thus imply which operation *exactly* has to be checked.
>
> I would guess something like
>
> int bla = overloadguard(expr);
>
> bla gets assigned the result of expression, and the outermost operation is checked in expr against overflow. Or maybe only allow expr to be an atomic expression, do not allow nesting.
>
> -eye
>

Parhaps someone could just define add(), mul(), sub() functions which could *either* return an exception or be written like:
int add(int l, int r, out bool overflow);

December 17, 2003
In article <brndrl$1dnt$1@digitaldaemon.com>, Ilya Minkov says...
>
>Roberto Mariottini wrote:
>> I realized that maybe an ordinary function like checkoverflow can be already done with inline assembly. Is it true?
>
>No, since you cannot rely upon execution order, and the compiler is free to insert any stuff between the statements it likes, which is very likely to zero the flags.
>
>The syntax must thus imply which operation *exactly* has to be checked.
>
>I would guess something like
>
>int bla = overloadguard(expr);
>
>bla gets assigned the result of expression, and the outermost operation is checked in expr against overflow. Or maybe only allow expr to be an atomic expression, do not allow nesting.

So you can have:

int x = oguard(x + oguard(y * z));

If oguard is inline expanded, I think it will do the job.

Do you have an idea of a possible implementation?

Ciao


December 17, 2003
>> How to check for overflow in integer operations?
>>
>> void main()
>> {
>> 	int a,b,c;
>> 	scanf("%ld %ld %ld", &a, &b, &c);
>> 	int d=a*b+c;
>> 	if(overflow) printf("error\n"); else printf("%ld",d);
>> }
>>
>
> or something like:
>
> void main()
> {
> 	int a,b,c;
> 	scanf("%ld %ld %ld", &a, &b, &c);
> 	try printf("%ld",a*b+c); catch(OverflowException) printf("error");
> }
>
> Can overflow chacking be done in D?
>

How about this syntax for compiler directives (similar to atributes)?

directive = (enable | disable) "(" identifier ")" (command | block | ":")

example:

enable(IntOverflowCheck)
{
	int func(int a, int b, int c)
	{
		b++; // checking
		disable(IntOverflowCheck) a*=2; // not checking
		return a*b+c; // checking
	}
}

or like this:
check(ArrayIndex) {}
uncheck(ArrayIndex) {}
check(IntOverflow) {}
December 17, 2003
I would like having this much control.  And a standard way to set options within the language would be nice...

Sean

"Tintor Marko" <elud@verat.net> wrote in message news:opr0cfaxdhlucekh@news.digitalmars.com...
> How about this syntax for compiler directives (similar to atributes)?
>
> directive = (enable | disable) "(" identifier ")" (command | block | ":")
>
> example:
>
> enable(IntOverflowCheck)
> {
> int func(int a, int b, int c)
> {
> b++; // checking
> disable(IntOverflowCheck) a*=2; // not checking
> return a*b+c; // checking
> }
> }
>
> or like this:
> check(ArrayIndex) {}
> uncheck(ArrayIndex) {}
> check(IntOverflow) {}


December 17, 2003
I would second this.  I would like a way to turn array bounds checking on and off.


In article <brql4n$mck$1@digitaldaemon.com>, Sean L. Palmer says...
>
>I would like having this much control.  And a standard way to set options within the language would be nice...
>
>Sean
>
>"Tintor Marko" <elud@verat.net> wrote in message news:opr0cfaxdhlucekh@news.digitalmars.com...
>> How about this syntax for compiler directives (similar to atributes)?
>>
>> directive = (enable | disable) "(" identifier ")" (command | block | ":")
>>
>> example:
>>
>> enable(IntOverflowCheck)
>> {
>> int func(int a, int b, int c)
>> {
>> b++; // checking
>> disable(IntOverflowCheck) a*=2; // not checking
>> return a*b+c; // checking
>> }
>> }
>>
>> or like this:
>> check(ArrayIndex) {}
>> uncheck(ArrayIndex) {}
>> check(IntOverflow) {}
>
>


1 2
Next ›   Last »