aboutsummaryrefslogtreecommitdiff
path: root/lobject.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-07-27 13:32:59 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-07-27 13:32:59 -0300
commit0acd55898d0aaae8dbc14c8a1bc1e3bdffc8701b (patch)
tree0ac634fed90877130b1f102bf4075af999de2158 /lobject.c
parent15231d4fb2f6984b25e0353ff46eda1a180b686d (diff)
downloadlua-0acd55898d0aaae8dbc14c8a1bc1e3bdffc8701b.tar.gz
lua-0acd55898d0aaae8dbc14c8a1bc1e3bdffc8701b.tar.bz2
lua-0acd55898d0aaae8dbc14c8a1bc1e3bdffc8701b.zip
Added gcc option '-Wconversion'
No warnings for standard numerical types. Still pending alternative numerical types.
Diffstat (limited to 'lobject.c')
-rw-r--r--lobject.c68
1 files changed, 36 insertions, 32 deletions
diff --git a/lobject.c b/lobject.c
index 45a27311..1c4ea1af 100644
--- a/lobject.c
+++ b/lobject.c
@@ -32,7 +32,7 @@
32/* 32/*
33** Computes ceil(log2(x)) 33** Computes ceil(log2(x))
34*/ 34*/
35int luaO_ceillog2 (unsigned int x) { 35lu_byte luaO_ceillog2 (unsigned int x) {
36 static const lu_byte log_2[256] = { /* log_2[i - 1] = ceil(log2(i)) */ 36 static const lu_byte log_2[256] = { /* log_2[i - 1] = ceil(log2(i)) */
37 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 37 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
38 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 38 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
@@ -46,7 +46,7 @@ int luaO_ceillog2 (unsigned int x) {
46 int l = 0; 46 int l = 0;
47 x--; 47 x--;
48 while (x >= 256) { l += 8; x >>= 8; } 48 while (x >= 256) { l += 8; x >>= 8; }
49 return l + log_2[x]; 49 return cast_byte(l + log_2[x]);
50} 50}
51 51
52/* 52/*
@@ -57,16 +57,19 @@ int luaO_ceillog2 (unsigned int x) {
57** to signal that. So, the real value is (1xxxx) * 2^(eeee - 7 - 1) if 57** to signal that. So, the real value is (1xxxx) * 2^(eeee - 7 - 1) if
58** eeee != 0, and (xxxx) * 2^-7 otherwise (subnormal numbers). 58** eeee != 0, and (xxxx) * 2^-7 otherwise (subnormal numbers).
59*/ 59*/
60unsigned int luaO_codeparam (unsigned int p) { 60lu_byte luaO_codeparam (unsigned int p) {
61 if (p >= (cast(lu_mem, 0x1F) << (0xF - 7 - 1)) * 100u) /* overflow? */ 61 if (p >= (cast(lu_mem, 0x1F) << (0xF - 7 - 1)) * 100u) /* overflow? */
62 return 0xFF; /* return maximum value */ 62 return 0xFF; /* return maximum value */
63 else { 63 else {
64 p = (cast(l_uint32, p) * 128 + 99) / 100; /* round up the division */ 64 p = (cast(l_uint32, p) * 128 + 99) / 100; /* round up the division */
65 if (p < 0x10) /* subnormal number? */ 65 if (p < 0x10) { /* subnormal number? */
66 return p; /* exponent bits are already zero; nothing else to do */ 66 /* exponent bits are already zero; nothing else to do */
67 else { 67 return cast_byte(p);
68 int log = luaO_ceillog2(p + 1) - 5; /* preserve 5 bits */ 68 }
69 return ((p >> log) - 0x10) | ((log + 1) << 4); 69 else { /* p >= 0x10 implies ceil(log2(p + 1)) >= 5 */
70 /* preserve 5 bits in 'p' */
71 unsigned log = luaO_ceillog2(p + 1) - 5u;
72 return cast_byte(((p >> log) - 0x10) | ((log + 1) << 4));
70 } 73 }
71 } 74 }
72} 75}
@@ -81,7 +84,7 @@ unsigned int luaO_codeparam (unsigned int p) {
81** more significant bits, as long as the multiplication does not 84** more significant bits, as long as the multiplication does not
82** overflow, so we check which order is best. 85** overflow, so we check which order is best.
83*/ 86*/
84l_obj luaO_applyparam (unsigned int p, l_obj x) { 87l_obj luaO_applyparam (lu_byte p, l_obj x) {
85 unsigned int m = p & 0xF; /* mantissa */ 88 unsigned int m = p & 0xF; /* mantissa */
86 int e = (p >> 4); /* exponent */ 89 int e = (p >> 4); /* exponent */
87 if (e > 0) { /* normalized? */ 90 if (e > 0) { /* normalized? */
@@ -189,9 +192,9 @@ void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
189} 192}
190 193
191 194
192int luaO_hexavalue (int c) { 195lu_byte luaO_hexavalue (int c) {
193 if (lisdigit(c)) return c - '0'; 196 if (lisdigit(c)) return cast_byte(c - '0');
194 else return (ltolower(c) - 'a') + 10; 197 else return cast_byte((ltolower(c) - 'a') + 10);
195} 198}
196 199
197 200
@@ -349,7 +352,7 @@ static const char *l_str2int (const char *s, lua_Integer *result) {
349 int d = *s - '0'; 352 int d = *s - '0';
350 if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */ 353 if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */
351 return NULL; /* do not accept it (as integer) */ 354 return NULL; /* do not accept it (as integer) */
352 a = a * 10 + d; 355 a = a * 10 + cast_uint(d);
353 empty = 0; 356 empty = 0;
354 } 357 }
355 } 358 }
@@ -373,7 +376,7 @@ size_t luaO_str2num (const char *s, TValue *o) {
373 } 376 }
374 else 377 else
375 return 0; /* conversion failed */ 378 return 0; /* conversion failed */
376 return (e - s) + 1; /* success; return string size */ 379 return ct_diff2sz(e - s) + 1; /* success; return string size */
377} 380}
378 381
379 382
@@ -409,7 +412,7 @@ int luaO_utf8esc (char *buff, unsigned long x) {
409/* 412/*
410** Convert a number object to a string, adding it to a buffer 413** Convert a number object to a string, adding it to a buffer
411*/ 414*/
412static int tostringbuff (TValue *obj, char *buff) { 415static unsigned tostringbuff (TValue *obj, char *buff) {
413 int len; 416 int len;
414 lua_assert(ttisnumber(obj)); 417 lua_assert(ttisnumber(obj));
415 if (ttisinteger(obj)) 418 if (ttisinteger(obj))
@@ -421,7 +424,7 @@ static int tostringbuff (TValue *obj, char *buff) {
421 buff[len++] = '0'; /* adds '.0' to result */ 424 buff[len++] = '0'; /* adds '.0' to result */
422 } 425 }
423 } 426 }
424 return len; 427 return cast_uint(len);
425} 428}
426 429
427 430
@@ -430,7 +433,7 @@ static int tostringbuff (TValue *obj, char *buff) {
430*/ 433*/
431void luaO_tostring (lua_State *L, TValue *obj) { 434void luaO_tostring (lua_State *L, TValue *obj) {
432 char buff[MAXNUMBER2STR]; 435 char buff[MAXNUMBER2STR];
433 int len = tostringbuff(obj, buff); 436 unsigned len = tostringbuff(obj, buff);
434 setsvalue(L, obj, luaS_newlstr(L, buff, len)); 437 setsvalue(L, obj, luaS_newlstr(L, buff, len));
435} 438}
436 439
@@ -448,13 +451,13 @@ void luaO_tostring (lua_State *L, TValue *obj) {
448** (LUA_IDSIZE + MAXNUMBER2STR) + a minimal space for basic messages, 451** (LUA_IDSIZE + MAXNUMBER2STR) + a minimal space for basic messages,
449** so that 'luaG_addinfo' can work directly on the buffer. 452** so that 'luaG_addinfo' can work directly on the buffer.
450*/ 453*/
451#define BUFVFS (LUA_IDSIZE + MAXNUMBER2STR + 95) 454#define BUFVFS cast_uint(LUA_IDSIZE + MAXNUMBER2STR + 95)
452 455
453/* buffer used by 'luaO_pushvfstring' */ 456/* buffer used by 'luaO_pushvfstring' */
454typedef struct BuffFS { 457typedef struct BuffFS {
455 lua_State *L; 458 lua_State *L;
456 int pushed; /* true if there is a part of the result on the stack */ 459 int pushed; /* true if there is a part of the result on the stack */
457 int blen; /* length of partial string in 'space' */ 460 unsigned blen; /* length of partial string in 'space' */
458 char space[BUFVFS]; /* holds last part of the result */ 461 char space[BUFVFS]; /* holds last part of the result */
459} BuffFS; 462} BuffFS;
460 463
@@ -492,7 +495,7 @@ static void clearbuff (BuffFS *buff) {
492** Get a space of size 'sz' in the buffer. If buffer has not enough 495** Get a space of size 'sz' in the buffer. If buffer has not enough
493** space, empty it. 'sz' must fit in an empty buffer. 496** space, empty it. 'sz' must fit in an empty buffer.
494*/ 497*/
495static char *getbuff (BuffFS *buff, int sz) { 498static char *getbuff (BuffFS *buff, unsigned sz) {
496 lua_assert(buff->blen <= BUFVFS); lua_assert(sz <= BUFVFS); 499 lua_assert(buff->blen <= BUFVFS); lua_assert(sz <= BUFVFS);
497 if (sz > BUFVFS - buff->blen) /* not enough space? */ 500 if (sz > BUFVFS - buff->blen) /* not enough space? */
498 clearbuff(buff); 501 clearbuff(buff);
@@ -509,9 +512,9 @@ static char *getbuff (BuffFS *buff, int sz) {
509*/ 512*/
510static void addstr2buff (BuffFS *buff, const char *str, size_t slen) { 513static void addstr2buff (BuffFS *buff, const char *str, size_t slen) {
511 if (slen <= BUFVFS) { /* does string fit into buffer? */ 514 if (slen <= BUFVFS) { /* does string fit into buffer? */
512 char *bf = getbuff(buff, cast_int(slen)); 515 char *bf = getbuff(buff, cast_uint(slen));
513 memcpy(bf, str, slen); /* add string to buffer */ 516 memcpy(bf, str, slen); /* add string to buffer */
514 addsize(buff, cast_int(slen)); 517 addsize(buff, cast_uint(slen));
515 } 518 }
516 else { /* string larger than buffer */ 519 else { /* string larger than buffer */
517 clearbuff(buff); /* string comes after buffer's content */ 520 clearbuff(buff); /* string comes after buffer's content */
@@ -525,7 +528,7 @@ static void addstr2buff (BuffFS *buff, const char *str, size_t slen) {
525*/ 528*/
526static void addnum2buff (BuffFS *buff, TValue *num) { 529static void addnum2buff (BuffFS *buff, TValue *num) {
527 char *numbuff = getbuff(buff, MAXNUMBER2STR); 530 char *numbuff = getbuff(buff, MAXNUMBER2STR);
528 int len = tostringbuff(num, numbuff); /* format number into 'numbuff' */ 531 unsigned len = tostringbuff(num, numbuff); /* format number into 'numbuff' */
529 addsize(buff, len); 532 addsize(buff, len);
530} 533}
531 534
@@ -537,10 +540,10 @@ static void addnum2buff (BuffFS *buff, TValue *num) {
537const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { 540const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
538 BuffFS buff; /* holds last part of the result */ 541 BuffFS buff; /* holds last part of the result */
539 const char *e; /* points to next '%' */ 542 const char *e; /* points to next '%' */
540 buff.pushed = buff.blen = 0; 543 buff.pushed = 0; buff.blen = 0;
541 buff.L = L; 544 buff.L = L;
542 while ((e = strchr(fmt, '%')) != NULL) { 545 while ((e = strchr(fmt, '%')) != NULL) {
543 addstr2buff(&buff, fmt, e - fmt); /* add 'fmt' up to '%' */ 546 addstr2buff(&buff, fmt, ct_diff2sz(e - fmt)); /* add 'fmt' up to '%' */
544 switch (*(e + 1)) { /* conversion specifier */ 547 switch (*(e + 1)) { /* conversion specifier */
545 case 's': { /* zero-terminated string */ 548 case 's': { /* zero-terminated string */
546 const char *s = va_arg(argp, char *); 549 const char *s = va_arg(argp, char *);
@@ -549,7 +552,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
549 break; 552 break;
550 } 553 }
551 case 'c': { /* an 'int' as a character */ 554 case 'c': { /* an 'int' as a character */
552 char c = cast_uchar(va_arg(argp, int)); 555 char c = cast_char(va_arg(argp, int));
553 addstr2buff(&buff, &c, sizeof(char)); 556 addstr2buff(&buff, &c, sizeof(char));
554 break; 557 break;
555 } 558 }
@@ -572,17 +575,17 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
572 break; 575 break;
573 } 576 }
574 case 'p': { /* a pointer */ 577 case 'p': { /* a pointer */
575 const int sz = 3 * sizeof(void*) + 8; /* enough space for '%p' */ 578 const unsigned sz = 3 * sizeof(void*) + 8; /* enough space for '%p' */
576 char *bf = getbuff(&buff, sz); 579 char *bf = getbuff(&buff, sz);
577 void *p = va_arg(argp, void *); 580 void *p = va_arg(argp, void *);
578 int len = lua_pointer2str(bf, sz, p); 581 int len = lua_pointer2str(bf, sz, p);
579 addsize(&buff, len); 582 addsize(&buff, cast_uint(len));
580 break; 583 break;
581 } 584 }
582 case 'U': { /* a 'long' as a UTF-8 sequence */ 585 case 'U': { /* an 'unsigned long' as a UTF-8 sequence */
583 char bf[UTF8BUFFSZ]; 586 char bf[UTF8BUFFSZ];
584 int len = luaO_utf8esc(bf, va_arg(argp, long)); 587 int len = luaO_utf8esc(bf, va_arg(argp, unsigned long));
585 addstr2buff(&buff, bf + UTF8BUFFSZ - len, len); 588 addstr2buff(&buff, bf + UTF8BUFFSZ - len, cast_uint(len));
586 break; 589 break;
587 } 590 }
588 case '%': { 591 case '%': {
@@ -648,7 +651,8 @@ void luaO_chunkid (char *out, const char *source, size_t srclen) {
648 addstr(out, source, srclen); /* keep it */ 651 addstr(out, source, srclen); /* keep it */
649 } 652 }
650 else { 653 else {
651 if (nl != NULL) srclen = nl - source; /* stop at first newline */ 654 if (nl != NULL)
655 srclen = ct_diff2sz(nl - source); /* stop at first newline */
652 if (srclen > bufflen) srclen = bufflen; 656 if (srclen > bufflen) srclen = bufflen;
653 addstr(out, source, srclen); 657 addstr(out, source, srclen);
654 addstr(out, RETS, LL(RETS)); 658 addstr(out, RETS, LL(RETS));