[qfcc] Handle signed-unsigned int comparison better

This fixes the upostop-- test by auto-casting implicit constants to
unsigned (and it gives a warning for signed-unsigned comparisons
otherwise). The generated code isn't quite the best, but the fix for
that is next.

Also clean up the resulting mess, though not properly. There are a few
bogus warnings, and the legit ones could do with a review.
This commit is contained in:
Bill Currie 2023-12-20 22:49:51 +09:00
parent 54839db826
commit dd183d3ba6
14 changed files with 72 additions and 54 deletions

View file

@ -10,15 +10,15 @@
*/ */
@interface MenuGroup : Group @interface MenuGroup : Group
{ {
int base; ///< The index of the first menu item. unsigned base; ///< The index of the first menu item.
int current; ///< The currently selected menu item. unsigned current; ///< The currently selected menu item.
} }
/** Set the index of the first menu item. /** Set the index of the first menu item.
\param b The index of the first menu item. \param b The index of the first menu item.
*/ */
-(void) setBase: (int) b; -(void) setBase: (unsigned) b;
/** Select the next menu item. /** Select the next menu item.

View file

@ -14,7 +14,7 @@
return self; return self;
} }
-(void)setBase:(int)b -(void)setBase:(unsigned)b
{ {
if (b >= [views count]) if (b >= [views count])
b = [views count] - 1; b = [views count] - 1;
@ -51,7 +51,7 @@
-(void) prev -(void) prev
{ {
if (--current < base) if (current-- == base)
current = [views count] - 1; current = [views count] - 1;
S_LocalSound ("misc/menu1.wav"); S_LocalSound ("misc/menu1.wav");
} }

View file

@ -22,8 +22,8 @@
while (i-- > 0) { while (i-- > 0) {
if (_objs[i] == anObject) { if (_objs[i] == anObject) {
for (tmp = i; tmp < count - 1; tmp++) { for (tmp = i + 1; tmp < count; tmp++) {
_objs[tmp] = _objs[tmp + 1]; _objs[tmp + 1] = _objs[tmp];
} }
count--; count--;
} }

View file

@ -52,10 +52,9 @@
+ (id) arrayWithObjects: (id *) objs count: (unsigned)cnt + (id) arrayWithObjects: (id *) objs count: (unsigned)cnt
{ {
local int i;
id newArray = [self array]; id newArray = [self array];
for (i = 0; i < cnt; i++) { for (unsigned i = 0; i < cnt; i++) {
[newArray addObject: (id) objs[i]]; [newArray addObject: (id) objs[i]];
} }
return newArray; return newArray;
@ -138,12 +137,10 @@
- (id) initWithObjects: (id *) objs count: (unsigned)cnt - (id) initWithObjects: (id *) objs count: (unsigned)cnt
{ {
local int i;
if (!(self = [self initWithCapacity: cnt])) if (!(self = [self initWithCapacity: cnt]))
return nil; return nil;
for (i = 0; i < cnt; i++) { for (unsigned i = 0; i < cnt; i++) {
[self addObject: (id) objs[i]]; [self addObject: (id) objs[i]];
} }
return self; return self;
@ -355,7 +352,6 @@
- (void) removeObjectAtIndex: (unsigned)index - (void) removeObjectAtIndex: (unsigned)index
{ {
local int i;
local id temp; local id temp;
if (index >= count) // FIXME: need exceptions if (index >= count) // FIXME: need exceptions
@ -363,7 +359,7 @@
temp = _objs[index]; temp = _objs[index];
count--; count--;
for (i = index; i < count; i++) { // reassign all objs >= index for (unsigned i = index; i < count; i++) { // reassign all objs >= index
_objs[i] = _objs[i+1]; _objs[i] = _objs[i+1];
} }
@ -393,9 +389,7 @@
- (void) makeObjectsPerformSelector: (SEL)selector - (void) makeObjectsPerformSelector: (SEL)selector
{ {
local int i; for (unsigned i = 0; i < [self count]; i++) {
for (i = 0; i < [self count]; i++) {
[[self objectAtIndex: i] performSelector: selector]; [[self objectAtIndex: i] performSelector: selector];
} }
} }
@ -403,9 +397,7 @@
- (void) makeObjectsPerformSelector: (SEL)selector - (void) makeObjectsPerformSelector: (SEL)selector
withObject: (void *)anObject withObject: (void *)anObject
{ {
local int i; for (unsigned i = 0; i < [self count]; i++) {
for (i = 0; i < [self count]; i++) {
[[self objectAtIndex: i] performSelector: selector withObject: anObject]; [[self objectAtIndex: i] performSelector: selector withObject: anObject];
} }
} }
@ -414,9 +406,7 @@
withObject: (void *)anObject withObject: (void *)anObject
withObject: (void *)anotherObject withObject: (void *)anotherObject
{ {
local int i; for (unsigned i = 0; i < [self count]; i++) {
for (i = 0; i < [self count]; i++) {
[[self objectAtIndex: i] performSelector: selector [[self objectAtIndex: i] performSelector: selector
withObject: anObject withObject: anObject
withObject: anotherObject]; withObject: anotherObject];

View file

@ -79,7 +79,7 @@ free_defs (LocalsData *self)
num_user_defs = 0; num_user_defs = 0;
if (aux_func) { if (aux_func) {
defs = qdb_get_local_defs (target, fnum); defs = qdb_get_local_defs (target, fnum);
for (int i = 0; i < aux_func.num_locals; i++) { for (unsigned i = 0; i < aux_func.num_locals; i++) {
string def_name = qdb_get_string (target, defs[i].name); string def_name = qdb_get_string (target, defs[i].name);
if (str_mid (def_name, 0, 1) != ".") { if (str_mid (def_name, 0, 1) != ".") {
num_user_defs++; num_user_defs++;
@ -88,7 +88,7 @@ free_defs (LocalsData *self)
def_views = obj_malloc (num_user_defs); def_views = obj_malloc (num_user_defs);
def_rows = obj_malloc (num_user_defs + 1); def_rows = obj_malloc (num_user_defs + 1);
def_rows[0] = 0; def_rows[0] = 0;
for (int i = 0, j = 0; i < aux_func.num_locals; i++) { for (unsigned i = 0, j = 0; i < aux_func.num_locals; i++) {
string def_name = qdb_get_string (target, defs[i].name); string def_name = qdb_get_string (target, defs[i].name);
if (str_mid (def_name, 0, 1) == ".") { if (str_mid (def_name, 0, 1) == ".") {
continue; continue;

View file

@ -10,7 +10,7 @@
static int static int
center (unsigned v, int len) center (unsigned v, int len)
{ {
return v > len / 2 ? v / 2 : 0; return v > (unsigned) (len / 2) ? v / 2 : 0;
} }
static void static void
@ -245,7 +245,7 @@ handleEvent (Editor *self, qwaq_event_t *event)
-scrollLeft:(unsigned) count -scrollLeft:(unsigned) count
{ {
if (base.x > count) { if ((unsigned) base.x > count) {
base.x -= count; base.x -= count;
} else { } else {
base.x = 0; base.x = 0;
@ -257,7 +257,7 @@ handleEvent (Editor *self, qwaq_event_t *event)
-scrollRight:(unsigned) count -scrollRight:(unsigned) count
{ {
if (1024 - base.x > count) { if ((unsigned) (1024 - base.x) > count) {
base.x += count; base.x += count;
} else { } else {
base.x = 1024; base.x = 1024;
@ -269,9 +269,9 @@ handleEvent (Editor *self, qwaq_event_t *event)
-scrollTo:(unsigned)target -scrollTo:(unsigned)target
{ {
if (target > base.y) { if (target > (unsigned) base.y) {
base_index = [buffer nextLine:base_index :target - base.y]; base_index = [buffer nextLine:base_index :target - base.y];
} else if (target < base.y) { } else if (target < (unsigned) base.y) {
base_index = [buffer prevLine:base_index :base.y - target]; base_index = [buffer prevLine:base_index :base.y - target];
} }
base.y = target; base.y = target;
@ -285,7 +285,7 @@ handleEvent (Editor *self, qwaq_event_t *event)
[self recenter:0]; [self recenter:0];
unsigned count = cursor.y; unsigned count = cursor.y;
if (count > ylen) { if (count > (unsigned) ylen) {
count = ylen; count = ylen;
} }
if (count) { if (count) {
@ -305,7 +305,7 @@ handleEvent (Editor *self, qwaq_event_t *event)
[self recenter:0]; [self recenter:0];
unsigned count = line_count - cursor.y; unsigned count = line_count - cursor.y;
if (count > ylen) { if (count > (unsigned) ylen) {
count = ylen; count = ylen;
} }
if (count) { if (count) {
@ -389,7 +389,7 @@ handleEvent (Editor *self, qwaq_event_t *event)
-charDown -charDown
{ {
[self recenter:0]; [self recenter:0];
if (cursor.y >= line_count) { if ((unsigned) cursor.y >= line_count) {
return self; return self;
} }
cursor.y++; cursor.y++;
@ -547,7 +547,7 @@ handleEvent (Editor *self, qwaq_event_t *event)
-moveEOS -moveEOS
{ {
unsigned count = line_count - base.y; unsigned count = line_count - base.y;
if (count > ylen - 1) { if (count > (unsigned) (ylen - 1)) {
count = ylen - 1; count = ylen - 1;
} }
line_index = [buffer nextLine:base_index :count]; line_index = [buffer nextLine:base_index :count];
@ -603,9 +603,9 @@ handleEvent (Editor *self, qwaq_event_t *event)
-gotoLine:(unsigned) line -gotoLine:(unsigned) line
{ {
if (line > cursor.y) { if (line > (unsigned) cursor.y) {
line_index = [buffer nextLine:line_index :line - cursor.y]; line_index = [buffer nextLine:line_index :line - cursor.y];
} else if (line < cursor.y) { } else if (line < (unsigned) cursor.y) {
line_index = [buffer prevLine:line_index :cursor.y - line]; line_index = [buffer prevLine:line_index :cursor.y - line];
} }
cursor.y = line; cursor.y = line;

View file

@ -7,7 +7,7 @@
if: (condition_func)condition if: (condition_func)condition
with: (void *)data with: (void *)data
{ {
for (int i = 0; i < [self count]; i++) { for (unsigned i = 0; i < [self count]; i++) {
if (condition (_objs[i], data)) { if (condition (_objs[i], data)) {
[_objs[i] performSelector: selector]; [_objs[i] performSelector: selector];
} }
@ -19,7 +19,7 @@
if: (condition_func2)condition if: (condition_func2)condition
with: (void *)data with: (void *)data
{ {
for (int i = 0; i < [self count]; i++) { for (unsigned i = 0; i < [self count]; i++) {
if (condition (_objs[i], anObject, data)) { if (condition (_objs[i], anObject, data)) {
[_objs[i] performSelector: selector withObject: anObject]; [_objs[i] performSelector: selector withObject: anObject];
} }

View file

@ -144,7 +144,7 @@ trySetFocus (Group *self, int viewIndex)
-selectNext -selectNext
{ {
for (int i = focused + 1; i < [views count]; i++) { for (unsigned i = focused + 1; i < [views count]; i++) {
if (trySetFocus (self, i)) { if (trySetFocus (self, i)) {
return self; return self;
} }

View file

@ -41,7 +41,7 @@
- (void) compile - (void) compile
{ {
local int index; local unsigned index;
local Instruction *inst; local Instruction *inst;
literals = [Frame newWithSize: [constants count] link: nil]; literals = [Frame newWithSize: [constants count] link: nil];
code = obj_malloc (@sizeof(instruction_t) * [instructions count]); code = obj_malloc (@sizeof(instruction_t) * [instructions count]);

View file

@ -18,9 +18,7 @@
- (int) indexLocal: (Symbol *) sym - (int) indexLocal: (Symbol *) sym
{ {
local int index; for (unsigned index = 0; index < [names count]; index++) {
for (index = 0; index < [names count]; index++) {
if (sym == [names objectAtIndex: index]) { if (sym == [names objectAtIndex: index]) {
return index; return index;
} }

View file

@ -60,6 +60,7 @@ static const expr_t *inverse_multiply (int op, const expr_t *e1,
const expr_t *e2); const expr_t *e2);
static const expr_t *double_compare (int op, const expr_t *e1, static const expr_t *double_compare (int op, const expr_t *e1,
const expr_t *e2); const expr_t *e2);
static const expr_t *uint_compare (int op, const expr_t *e1, const expr_t *e2);
static const expr_t *vector_compare (int op, const expr_t *e1, const expr_t *e2); static const expr_t *vector_compare (int op, const expr_t *e1, const expr_t *e2);
static const expr_t *vector_dot (int op, const expr_t *e1, const expr_t *e2); static const expr_t *vector_dot (int op, const expr_t *e1, const expr_t *e2);
static const expr_t *vector_multiply (int op, const expr_t *e1, const expr_t *e2); static const expr_t *vector_multiply (int op, const expr_t *e1, const expr_t *e2);
@ -391,10 +392,10 @@ static expr_type_t int_uint[] = {
{SHR, &type_int, 0, &type_int}, {SHR, &type_int, 0, &type_int},
{EQ, &type_int, 0, &type_int}, {EQ, &type_int, 0, &type_int},
{NE, &type_int, 0, &type_int}, {NE, &type_int, 0, &type_int},
{LE, &type_int, 0, &type_int}, {LE, .process = uint_compare},
{GE, &type_int, 0, &type_int}, {GE, .process = uint_compare},
{LT, &type_int, 0, &type_int}, {LT, .process = uint_compare},
{GT, &type_int, 0, &type_int}, {GT, .process = uint_compare},
{0, 0} {0, 0}
}; };
@ -469,10 +470,10 @@ static expr_type_t uint_int[] = {
{SHR, &type_uint, 0, &type_int }, {SHR, &type_uint, 0, &type_int },
{EQ, &type_int, &type_int, &type_int }, {EQ, &type_int, &type_int, &type_int },
{NE, &type_int, &type_int, &type_int }, {NE, &type_int, &type_int, &type_int },
{LE, &type_int, &type_int, &type_int }, {LE, .process = uint_compare},
{GE, &type_int, &type_int, &type_int }, {GE, .process = uint_compare},
{LT, &type_int, &type_int, &type_int }, {LT, .process = uint_compare},
{GT, &type_int, &type_int, &type_int }, {GT, .process = uint_compare},
{0, 0} {0, 0}
}; };
@ -1072,6 +1073,34 @@ double_compare (int op, const expr_t *e1, const expr_t *e2)
return e; return e;
} }
static const expr_t *
uint_compare (int op, const expr_t *e1, const expr_t *e2)
{
type_t *t1 = get_type (e1);
type_t *t2 = get_type (e2);
expr_t *e;
if (is_constant (e1) && e1->implicit && is_int (t1)) {
t1 = &type_uint;
e1 = cast_expr (t1, e1);
}
if (is_constant (e2) && e2->implicit && is_int (t2)) {
t2 = &type_uint;
e2 = cast_expr (t2, e2);
}
if (t1 != t2) {
warning (e1, "comparison between signed and unsigned");
if (is_int (t1)) {
e1 = cast_expr (&type_uint, e2);
} else {
e2 = cast_expr (&type_uint, e2);
}
}
e = new_binary_expr (op, e1, e2);
e->expr.type = &type_int;
return e;
}
static const expr_t * static const expr_t *
entity_compare (int op, const expr_t *e1, const expr_t *e2) entity_compare (int op, const expr_t *e1, const expr_t *e2)
{ {

View file

@ -41,7 +41,7 @@ find_xdef (string varname)
//FIXME need a simple way to get at a def's meta-data //FIXME need a simple way to get at a def's meta-data
xdefs_t *xdefs = PR_FindGlobal (".xdefs"); xdefs_t *xdefs = PR_FindGlobal (".xdefs");
xdef_t *xdef = xdefs.xdefs; xdef_t *xdef = xdefs.xdefs;
while (xdef - xdefs.xdefs < xdefs.num_xdefs && xdef.addr != varaddr) { while (xdef - xdefs.xdefs < (int) xdefs.num_xdefs && xdef.addr != varaddr) {
xdef++; xdef++;
} }
if (xdef.addr != varaddr) { if (xdef.addr != varaddr) {

View file

@ -23,7 +23,8 @@ main (void)
//FIXME need a simple way to get at a def's meta-data //FIXME need a simple way to get at a def's meta-data
xdefs_t *xdefs = PR_FindGlobal (".xdefs"); xdefs_t *xdefs = PR_FindGlobal (".xdefs");
xdef_t *xdef = xdefs.xdefs; xdef_t *xdef = xdefs.xdefs;
while (xdef - xdefs.xdefs < xdefs.num_xdefs && xdef.ofs != &int32_ptr) { while (xdef - xdefs.xdefs < (int) xdefs.num_xdefs
&& xdef.ofs != &int32_ptr) {
xdef++; xdef++;
} }
printf ("int32_ptr: %s\n", xdef.type.encoding); printf ("int32_ptr: %s\n", xdef.type.encoding);

View file

@ -28,7 +28,7 @@ find_xdef (void *varaddr)
//FIXME need a simple way to get at a def's meta-data //FIXME need a simple way to get at a def's meta-data
xdefs_t *xdefs = PR_FindGlobal (".xdefs"); xdefs_t *xdefs = PR_FindGlobal (".xdefs");
xdef_t *xdef = xdefs.xdefs; xdef_t *xdef = xdefs.xdefs;
while (xdef - xdefs.xdefs < xdefs.num_xdefs && xdef.ofs != varaddr) { while (xdef - xdefs.xdefs < (int) xdefs.num_xdefs && xdef.ofs != varaddr) {
xdef++; xdef++;
} }
if (xdef.ofs != varaddr) { if (xdef.ofs != varaddr) {