|
|
#1 |
|
Adept
Join Date: Jan 2008
Posts: 227
![]() |
Help: Bad Code Generation?
Hi,
I'm wondering if anyone can help me with what looks to be a compiler bug. The symptom is that Code:
lookup_kind(TV_FLASK, SV_ANY); Here is the code: Code:
s16b lookup_kind(int tval, int sval)
{
int k;
int num = 0;
int bk = 0;
/* Look for it */
for (k = 1; k < max_k_idx; k++)
{
object_kind *k_ptr = &k_info[k];
/* Require correct tval */
if (k_ptr->tval != tval) continue;
msg_format("Found tval = %d, sval = %d, num = %d", k_ptr->tval, k_ptr->sval, num);
/* Found a match */
if (k_ptr->sval == sval) return (k);
/* Ignore illegal items */
if (sval != SV_ANY) continue;
/* Apply the randomizer */
if (!one_in_(++num)) continue;
/* Use this value */
msg_format("Set bk = %d", k);
bk = k;
}
/* Return this choice */
if (sval == SV_ANY)
{
return bk;
}
#if 0
/* Oops */
#ifdef JP
msg_format("Æà¬Ê (%d,%d)", tval, sval);
#else
msg_format("No object (%d,%d)", tval, sval);
#endif
#endif
/* Oops */
return (0);
}
Here is disassembly: Code:
/* Apply the randomizer */ if (!one_in_(++num)) continue; 01252948 inc ebx 01252949 test ebx,ebx 0125294B jle lookup_kind+7Bh (125295Bh) 0125294D inc ebx 0125294E push ebx 0125294F call Rand_div (12D8690h) Any thoughts on this one? Would you like to see more disassembly? Frankly, I suck at assembly and can't make heads or tails of the machine code generated for this routine, but it is clearly incorrect. Thanks in advance, chris |
|
|
|
|
|
#2 |
|
Adept
Join Date: Jan 2008
Posts: 227
![]() |
Never mind:
Code:
#define one_in_(X) \ (X <= 0 || randint0(X) == 0) Code:
(++num <= 0 || randint0(++num) == 0) chris |
|
|
|
|
|
#3 |
|
Prophet
Join Date: Dec 2009
Posts: 4,745
![]() |
You have a macro that replaces "X" with "++num"? Yikes.
|
|
|
|
|
|
#4 |
|
Swordsman
Join Date: Jun 2007
Posts: 340
![]() |
|
|
|
|
|
|
#5 |
|
Prophet
Join Date: Dec 2009
Posts: 4,745
![]() |
Ah, quite right. "++num" was the argument to the macro, so it's evaluated twice since it occurs twice in the macro. My mistake.
Macros aren't a terrible idea in principle, but they really need to have a different calling convention than normal functions, because their behavior is totally different. |
|
|
|
|
|
#6 |
|
Adept
Join Date: Jan 2008
Posts: 227
![]() |
Sorry, I meant that
Code:
if (!one_in_(++num)) continue Code:
if (!((++num) <= 0 || randint0((++num)) == 0)) continue; Code:
#define one_in_(X) ((X) <= 0 || randint0((X)) == 0) |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Is this just really bad luck? | Therem Harth | Vanilla | 17 | December 11, 2011 17:52 |
| Using bad items offensively? | Starhawk | Vanilla | 27 | January 31, 2011 00:48 |
| Bad pointer in vcmd_insert_repeated | Sirridan | Development | 1 | August 24, 2010 18:11 |
| Bad memory read | jbu | Development | 1 | August 14, 2010 13:57 |
| Selling bad weapons | z118 | Vanilla | 10 | February 14, 2010 11:02 |