Commit Graph

747 Commits

Author SHA1 Message Date
Roberto Ierusalimschy
7d4c7ae2af Changes in opcodes for generic 'for'
Again, as the control variable is read only, the code doesn't need
to keep an internal copy of it.
2022-12-22 13:52:53 -03:00
Roberto Ierusalimschy
873588dc5f Simplification in opcodes for numerical 'for'
As the control variable is read only, the code doesn't need to keep
an internal copy of it.
2022-12-21 15:35:40 -03:00
Roberto Ierusalimschy
413a393e62 Stack indices changed to union's
That will allow to change pointers to offsets while reallocating
the stack.
2022-10-29 12:06:37 -03:00
Roberto Ierusalimschy
26be27459b Negation in constant folding of '>>' may overflow 2022-09-23 11:08:10 -03:00
Roberto Ierusalimschy
603b2c64ad 'luaV_concat' can use invalidated pointer to stack
Bug introduced in commit 42d40581.
2022-05-23 17:50:47 -03:00
Roberto Ierusalimschy
42d40581dd Save stack space while handling errors
Because error handling (luaG_errormsg) uses slots from EXTRA_STACK,
and some errors can recur (e.g., string overflow while creating an
error message in 'luaG_runerror', or a C-stack overflow before calling
the message handler), the code should use stack slots with parsimony.

This commit fixes the bug "Lua-stack overflow when C stack overflows
while handling an error".
2022-05-20 13:14:33 -03:00
Roberto Ierusalimschy
f3cfd5bf2b Details
Comments + manual + identation + asserts about stack limits that were
not allowing the use of the full stack
2022-04-01 13:55:44 -03:00
Roberto Ierusalimschy
8426d9b4d4 Avoid computing invalid addresses
luaV_execute should compute 'ra' only when the instruction uses it.
Computing an illegal address is undefined behavior even if the address
is never dereferenced.
2022-02-18 13:22:25 -03:00
Roberto Ierusalimschy
1fce5bea81 More uniform implementation for tail calls
'luaD_pretailcall' mimics 'luaD_precall', handling call metamethods
and calling C functions directly. That makes the code in the
interpreter loop simpler.

This commit also goes back to emulating the tail call in 'luaD_precall'
with a goto, as C compilers may not do proper tail calls and the C
stack can overflow much sooner than the Lua stack (which grows as the
metamethod is added to it).
2021-10-29 13:41:24 -03:00
Roberto Ierusalimschy
87a9573b2e Documentation
Better explanation about the guaranties of multiple assignment in
the manual.
2021-10-11 13:49:13 -03:00
Roberto Ierusalimschy
2ff3471722 Using 'inline' in some functions
According to ISO C, "making a function an inline function suggests that
calls to the function be as fast as possible." (Not available in C89.)
2021-09-15 11:18:41 -03:00
Roberto Ierusalimschy
91673a8ec0 'luaD_tryfuncTM' checks stack space by itself 2021-08-18 12:05:06 -03:00
Roberto Ierusalimschy
41871f1803 Undo simplification of tail calls (commit 901d760)
Not that simpler and slower.
2021-08-18 11:21:33 -03:00
Roberto Ierusalimschy
62fb934427 Bug: Negation in 'luaV_shiftr' may overflow
Negation of an unchecked lua_Integer overflows with mininteger.
2021-07-22 13:44:53 -03:00
Roberto Ierusalimschy
dbdc74dc55 Simplification in the parameters of 'luaD_precall'
The parameters 'nresults' and 'delta1', in 'luaD_precall', were never
meaningful simultaneously. So, they were combined in a single parameter
'retdel'.
2021-06-30 12:53:21 -03:00
Roberto Ierusalimschy
04e19712a5 C functions can be tail called, too
A tail call to a C function can have the behavior of a "real" tail
call, reusing the stack frame of the caller.
2021-06-14 13:28:21 -03:00
Roberto Ierusalimschy
901d760093 Simpler implementation for tail calls
Tail calls handled by 'luaD_precall', like regular calls, to avoid
code duplication.
2021-06-11 13:41:07 -03:00
Roberto Ierusalimschy
681297187e Bug: yielding in '__close' mess up number of returns
Yielding in a __close metamethod called when returning vararg results
changes the top and so messes up the number of returned values.
2021-04-16 15:41:44 -03:00
Roberto Ierusalimschy
d205f3a484 Bug: Lua source should not use C99 comments ("//") 2021-04-10 10:19:21 -03:00
Roberto Ierusalimschy
5205f073c5 Don't use tointegerns when luaV_tointegerns will do
Some places don't need the "fast path" macro tointegerns, either
because speed is not essential (lcode.c) or because the value is not
supposed to be an integer already (luaV_equalobj and luaG_tointerror).
Moreover, luaV_equalobj should always use F2Ieq, even if Lua is
compiled to "round to floor".
2021-02-24 12:24:42 -03:00
Roberto Ierusalimschy
31925e4cc2 Details
Added documentation and asserts that constants for arithmetic opcodes
must be numbers.
2021-02-24 11:30:46 -03:00
Roberto Ierusalimschy
59c88f846d Broadening the use of branch hints
More uses of macros 'likely'/'unlikely' (renamed to
'l_likely'/'l_unlikely'), both in range (extended to the
libraries) and in scope (extended to hooks, stack growth).
2021-02-24 11:14:44 -03:00
Roberto Ierusalimschy
4e47f81188 New implementation for to-be-closed variables
To-be-closed variables are linked in their own list, embedded into the
stack elements. (Due to alignment, this information does not change
the size of the stack elements in most architectures.)  This new list
does not produce garbage and avoids memory errors when creating tbc
variables.
2021-02-09 14:00:05 -03:00
Roberto Ierusalimschy
2bfa13e520 Fixed some bugs around stack reallocation
Long time without using HARDSTACKTESTS...
2021-02-05 11:00:28 -03:00
Roberto Ierusalimschy
b07fc10e91 Allow yields inside '__close' metamethods
Initial implementation to allow yields inside '__close' metamethods.
This current version still does not allow a '__close' metamethod
to yield when called due to an error. '__close' metamethods from
C functions also are not allowed to yield.
2021-01-13 13:54:10 -03:00
Roberto Ierusalimschy
7af27ef59d Cleaner handling of errors in '__close' metamethods
Instead of protecting each individual metamethod call, protect the
entire call to 'luaF_close'.
2020-12-28 11:40:30 -03:00
Roberto Ierusalimschy
23051e830a Changes in the API of 'luaH_set' and related functions
Functions to set values in a table (luaH_set, luaH_newkey, etc.) receive
the new value, instead of returning a slot where to put the value.
2020-12-04 11:08:42 -03:00
Roberto Ierusalimschy
e4a38eb0e8 Fixed wrong trace of vararg functions
Trace of vararg functions was skipping an instruction when returning
from a call. (Bug introduced by commit 5d8ce05b3.)
2020-10-19 15:55:25 -03:00
Roberto Ierusalimschy
5aa36e894f No more field 'lua_State.stacksize'
The stack size is derived from 'stack_last', when needed. Moreover,
the handling of stack sizes is more consistent, always excluding the
extra space except when allocating/deallocating the array.
2020-10-12 12:29:09 -03:00
Roberto Ierusalimschy
490d42b5f8 Correct handling of 'luaV_execute' invocations
The previous stackless implementations marked all 'luaV_execute'
invocations as fresh. However, re-entering 'luaV_execute' when
resuming a coroutine should not be a fresh invocation. (It works
because 'unroll' called 'luaV_execute' for each call entry, but
it was slower than letting 'luaV_execute' finish all non-fresh
invocations.)
2020-10-12 12:29:09 -03:00
Roberto Ierusalimschy
287b302acb Revision of stackless implementation
- more organized handling of 'nCcalls'
- comments
- deprecation of 'setcstacklimit'
2020-10-12 12:29:09 -03:00
Roberto Ierusalimschy
5d8ce05b3f Back to a stackless implementation
A "with stack" implementation gains too little in performance to be
worth all the noise from C-stack overflows.

This commit is almost a sketch, to test performance. There are several
pending stuff:

- review control of C-stack overflow and error messages;
- what to do with setcstacklimit;
- review comments;
- review unroll of Lua calls.
2020-10-12 12:29:09 -03:00
Roberto Ierusalimschy
a2195644d8 Fixed bug: invalid 'oldpc' when returning to a function
The field 'L->oldpc' is not always updated when control returns to a
function; an invalid value can seg. fault when computing 'changedline'.
(One example is an error in a finalizer; control can return to
'luaV_execute' without executing 'luaD_poscall'.) Instead of trying to
fix all possible corner cases, it seems safer to be resilient to invalid
values for 'oldpc'. Valid but wrong values at most cause an extra call
to a line hook.
2020-07-17 11:01:05 -03:00
Roberto Ierusalimschy
31b8c2d438 Fixed bug of access violation in finalizers
Errors in finalizers need a valid 'pc' to produce an error message,
even if the error is not propagated. Therefore, calls to the GC (which
may call finalizers) inside luaV_execute must save the 'pc'.
2020-07-08 12:02:56 -03:00
Roberto Ierusalimschy
eb41999461 Fixed bugs of stack reallocation x GC
Macro 'checkstackGC' was doing a GC step after resizing the stack;
the GC could shrink the stack and undo the resize. Moreover, macro
'checkstackp' also does a GC step, which could remove the preallocated
CallInfo when calling a function. (Its name has been changed to
'checkstackGCp' to emphasize that it calls the GC.)
2020-07-07 18:03:48 -03:00
Roberto Ierusalimschy
ae809e9fd1 'luaV_concat' can "concat" one single value
Several of its callers needed that case and had to do the check
themselves.
2020-07-03 11:54:58 -03:00
Roberto Ierusalimschy
6eb53b7526 Details
Several details in code (e.g., moving a variable to the most inner
scope that encloses its uses), comments, parameter names, extra tests.
2020-02-27 12:59:22 -03:00
Roberto Ierusalimschy
9b7987a9d1 OP_LOADFALSE broken in two instructions 2020-02-11 11:12:33 -03:00
Roberto Ierusalimschy
46c3587a6f Clearer distinction between types and tags
LUA_T* represents only types; tags (types + Variants) are represented
by LUA_V* constants.
2020-01-31 11:09:53 -03:00
Roberto Ierusalimschy
5ff408d218 Changed internal representation of booleans
Instead of an explicit value (field 'b'), true and false use different
tag variants. This avoids reading an extra field and results in more
direct code. (Most code that uses booleans needs to distinguish between
true and false anyway.)
2020-01-06 11:38:31 -03:00
Roberto Ierusalimschy
1e0ad018ce Comment about LUA_COMPAT_LT_LE moved to proper place 2019-12-10 13:50:20 -03:00
Roberto Ierusalimschy
95735bda46 Simplifications in 'op_arith*' family of macros 2019-12-05 14:51:58 -03:00
Roberto Ierusalimschy
d30569c064 Using an enumeration for float->integer coercion modes 2019-12-05 14:14:29 -03:00
Roberto Ierusalimschy
2d92102dee 'l_mathlim' renamed to 'l_floatatt'
That macro is applied to float attributes, not to limits.
2019-12-05 13:31:07 -03:00
Roberto Ierusalimschy
490ecfcaa1 Better comments about the use of 'k' in opcodes 2019-12-05 12:59:42 -03:00
Roberto Ierusalimschy
81f2401c6d Code reorganization for opcodes OP_FORPREP and OP_FORLOOP
Parts of the code for opcodes OP_FORPREP and OP_FORLOOP were moved
to functions outside the interpreter loop.
2019-12-04 16:51:53 -03:00
Roberto Ierusalimschy
5f83fb6582 Details 2019-11-18 14:54:06 -03:00
Roberto Ierusalimschy
7d526e75a7 Fixed bug in tail calls of __call chains
A tail call of a __call chain (a __call metamethod that itself is
also not a function) was being perfomed as a regular call.
2019-10-28 15:58:07 -03:00
Roberto Ierusalimschy
c12983cf8a Fixed warnings from Keil compiler 2019-10-25 17:41:40 -03:00
Roberto Ierusalimschy
91dad09f65 Removed arithmetic opcodes with immediate operand
The difference in performance between immediate operands and K operands
does not seem to justify all those extra opcodes. We only keep OP_ADDI,
due to its ubiquity and because the difference is a little more relevant.
(Later, OP_SUBI will be implemented by OP_ADDI, negating the constant.)
2019-09-10 13:20:03 -03:00