Skip to content

Commit

Permalink
Fix functions with statics in constants
Browse files Browse the repository at this point in the history
Also add a test to ensure that new static variable semantics work as intended.
  • Loading branch information
GrieferAtWork committed Mar 30, 2024
1 parent 7bae9fe commit 35e0287
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 134 deletions.
3 changes: 2 additions & 1 deletion .vs/deemon.sln
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{2360741E-F
..\util\test\compiler-self-recursion.dee = ..\util\test\compiler-self-recursion.dee
..\util\test\compiler-sequence-cast.dee = ..\util\test\compiler-sequence-cast.dee
..\util\test\compiler-sequence-copy.dee = ..\util\test\compiler-sequence-copy.dee
..\util\test\compiler-static-variables.dee = ..\util\test\compiler-static-variables.dee
..\util\test\compiler-template-string-jit.dee = ..\util\test\compiler-template-string-jit.dee
..\util\test\compiler-template-string.dee = ..\util\test\compiler-template-string.dee
..\util\test\compiler-thousands-grouping.dee = ..\util\test\compiler-thousands-grouping.dee
Expand All @@ -361,7 +362,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{2360741E-F
..\util\test\ctypes-from-bytes.dee = ..\util\test\ctypes-from-bytes.dee
..\util\test\ctypes-futex.dee = ..\util\test\ctypes-futex.dee
..\util\test\ctypes.dee = ..\util\test\ctypes.dee
..\util\test\deemon-bytes-replace.dee = ..\util\test\deemon-bytes-replace.dee
..\util\test\deemon-dict-frozen.dee = ..\util\test\deemon-dict-frozen.dee
..\util\test\deemon-dict-update.dee = ..\util\test\deemon-dict-update.dee
..\util\test\deemon-float.dee = ..\util\test\deemon-float.dee
Expand Down Expand Up @@ -400,6 +400,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{2360741E-F
..\util\test\deemon-string-quote.dee = ..\util\test\deemon-string-quote.dee
..\util\test\deemon-string-regex.dee = ..\util\test\deemon-string-regex.dee
..\util\test\deemon-string-repeat.dee = ..\util\test\deemon-string-repeat.dee
..\util\test\deemon-string-replace.dee = ..\util\test\deemon-string-replace.dee
..\util\test\deemon-string-scanf.dee = ..\util\test\deemon-string-scanf.dee
..\util\test\deemon-string-sstrip.dee = ..\util\test\deemon-string-sstrip.dee
..\util\test\deemon-string-strip.dee = ..\util\test\deemon-string-strip.dee
Expand Down
96 changes: 48 additions & 48 deletions .vs/deemon.vcxproj.user
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug-MSVC (NDEBUG)|Win32'">
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
<LocalDebuggerCommandArguments>-O3 input.dee</LocalDebuggerCommandArguments>
<LocalDebuggerCommand>$(ProjectDir)..\deemon.exe</LocalDebuggerCommand>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug-MSVC (NDEBUG)|x64'">
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
<LocalDebuggerCommandArguments>-O3 input.dee</LocalDebuggerCommandArguments>
<LocalDebuggerCommand>$(ProjectDir)..\deemon.exe</LocalDebuggerCommand>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug-MSVC|Win32'">
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
<LocalDebuggerCommandArguments>-O3 input.dee</LocalDebuggerCommandArguments>
<LocalDebuggerCommand>$(ProjectDir)..\deemon.exe</LocalDebuggerCommand>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug-MSVC|x64'">
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
<LocalDebuggerCommandArguments>-O3 input.dee</LocalDebuggerCommandArguments>
<LocalDebuggerCommand>$(ProjectDir)..\deemon.exe</LocalDebuggerCommand>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release-MSVC (NDEBUG)|Win32'">
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
<LocalDebuggerCommandArguments>-O3 input.dee</LocalDebuggerCommandArguments>
<LocalDebuggerCommand>$(ProjectDir)..\deemon.exe</LocalDebuggerCommand>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release-MSVC (NDEBUG)|x64'">
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
<LocalDebuggerCommandArguments>-O3 input.dee</LocalDebuggerCommandArguments>
<LocalDebuggerCommand>$(ProjectDir)..\deemon.exe</LocalDebuggerCommand>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release-MSVC|Win32'">
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
<LocalDebuggerCommandArguments>-O3 input.dee</LocalDebuggerCommandArguments>
<LocalDebuggerCommand>$(ProjectDir)..\deemon.exe</LocalDebuggerCommand>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release-MSVC|x64'">
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
<LocalDebuggerCommandArguments>-O3 input.dee</LocalDebuggerCommandArguments>
<LocalDebuggerCommand>$(ProjectDir)..\deemon.exe</LocalDebuggerCommand>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug-MSVC (NDEBUG)|Win32'">
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
<LocalDebuggerCommandArguments>-O3 input.dee</LocalDebuggerCommandArguments>
<LocalDebuggerCommand>$(ProjectDir)..\deemon.exe</LocalDebuggerCommand>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug-MSVC (NDEBUG)|x64'">
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
<LocalDebuggerCommandArguments>-O3 input.dee</LocalDebuggerCommandArguments>
<LocalDebuggerCommand>$(ProjectDir)..\deemon.exe</LocalDebuggerCommand>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug-MSVC|Win32'">
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
<LocalDebuggerCommandArguments>-O3 input.dee</LocalDebuggerCommandArguments>
<LocalDebuggerCommand>$(ProjectDir)..\deemon.exe</LocalDebuggerCommand>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug-MSVC|x64'">
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
<LocalDebuggerCommandArguments>-O3 input.dee</LocalDebuggerCommandArguments>
<LocalDebuggerCommand>$(ProjectDir)..\deemon.exe</LocalDebuggerCommand>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release-MSVC (NDEBUG)|Win32'">
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
<LocalDebuggerCommandArguments>-O3 input.dee</LocalDebuggerCommandArguments>
<LocalDebuggerCommand>$(ProjectDir)..\deemon.exe</LocalDebuggerCommand>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release-MSVC (NDEBUG)|x64'">
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
<LocalDebuggerCommandArguments>-O3 input.dee</LocalDebuggerCommandArguments>
<LocalDebuggerCommand>$(ProjectDir)..\deemon.exe</LocalDebuggerCommand>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release-MSVC|Win32'">
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
<LocalDebuggerCommandArguments>-O3 input.dee</LocalDebuggerCommandArguments>
<LocalDebuggerCommand>$(ProjectDir)..\deemon.exe</LocalDebuggerCommand>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release-MSVC|x64'">
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
<LocalDebuggerCommandArguments>-O3 input.dee</LocalDebuggerCommandArguments>
<LocalDebuggerCommand>$(ProjectDir)..\deemon.exe</LocalDebuggerCommand>
</PropertyGroup>
</Project>
16 changes: 8 additions & 8 deletions include/deemon/asm.h
Original file line number Diff line number Diff line change
Expand Up @@ -832,10 +832,10 @@
#define ASM_CALLCMEMBER_THIS_R 0x6b/* [4][-n,+1] `push callcmember this, ref <imm8>, $<imm8>, #<imm8>'- Lookup a class member of `ref <imm8>', given its index, then call that attribute as a this-call by popping #<imm8> arguments. */
/* ASM_ 0x6c * -------- - ------------------ */
/* ASM_ 0x6d * -------- - ------------------ */
#define ASM_FUNCTION_C 0x6e /* [3][-n,+1] `push function const <imm8>, #<imm8>+1' - Create a new function Object, using constant slot <imm8> as code and popping $<imm8>+1 objects for use as references.
* [3][-n,+0] `PREFIX: function const <imm8>, #<imm8>+1' */
#define ASM_FUNCTION_C_16 0x6f /* [4][-n,+1] `push function const <imm8>, #<imm16>+1' - Create a new function Object, using constant slot <imm8> as code and popping $<imm16>+1 objects for use as references.
* [4][-n,+0] `PREFIX: function const <imm8>, #<imm16>+1' */
#define ASM_FUNCTION_C 0x6e /* [3][-n,+1] `push function const <imm8>, #<imm8>' - Create a new function Object, using constant slot <imm8> as code and popping $<imm8> objects for use as references.
* [3][-n,+0] `PREFIX: function const <imm8>, #<imm8>' */
#define ASM_FUNCTION_C_16 0x6f /* [4][-n,+1] `push function const <imm8>, #<imm16>' - Create a new function Object, using constant slot <imm8> as code and popping $<imm16> objects for use as references.
* [4][-n,+0] `PREFIX: function const <imm8>, #<imm16>' */

/* Arithmetic instructions (NOTE: Some of these may be prefixed to perform an inplace operation). */
#define ASM_CAST_INT 0x70 /* [1][-1,+1] `cast top, int' - Convert the top-most stack entry into an integer. */
Expand Down Expand Up @@ -1337,10 +1337,10 @@
#define ASM16_CALLCMEMBER_THIS_R 0xf06b/*[7][-n,+1] `push callcmember this, ref <imm16>, $<imm16>, #<imm8>'- Lookup a class member of `ref <imm16>', given its index, then call that attribute as a this-call by popping #<imm8> arguments. */
/* ASM_ 0xf06c * -------- - ------------------ */
/* ASM_ 0xf06d * -------- - ------------------ */
#define ASM16_FUNCTION_C 0xf06e /* [5][-n,+1] `push function const <imm16>, #<imm8>+1' - Create a new function Object, using constant slot <imm16> as code and popping $<imm8>+1 objects for use by references.
* [5][-n,+0] `PREFIX: function const <imm16>, #<imm8>+1' */
#define ASM16_FUNCTION_C_16 0xf06f /* [6][-n,+1] `push function const <imm16>, #<imm16>+1' - Create a new function Object, using constant slot <imm16> as code and popping $<imm16>+1 objects for use by references.
* [6][-n,+0] `PREFIX: function const <imm16>, #<imm16>+1' */
#define ASM16_FUNCTION_C 0xf06e /* [5][-n,+1] `push function const <imm16>, #<imm8>' - Create a new function Object, using constant slot <imm16> as code and popping $<imm8> objects for use by references.
* [5][-n,+0] `PREFIX: function const <imm16>, #<imm8>' */
#define ASM16_FUNCTION_C_16 0xf06f /* [6][-n,+1] `push function const <imm16>, #<imm16>' - Create a new function Object, using constant slot <imm16> as code and popping $<imm16> objects for use by references.
* [6][-n,+0] `PREFIX: function const <imm16>, #<imm16>' */
#define ASM_SUPERGETATTR_THIS_RC 0xf070/*[4][-0,+1] `push getattr this, ref <imm8>, const <imm8>' - Perform a super attribute lookup `(this as ref <imm8>).operator . (const <imm8>)'. */
#define ASM16_SUPERGETATTR_THIS_RC 0xf071/*[6][-0,+1]`push getattr this, ref <imm16>, const <imm16>' - Perform a super attribute lookup `(this as ref <imm16>).operator . (const <imm16>)'. */
#define ASM_SUPERCALLATTR_THIS_RC 0xf072/*[5][-n,+1] `push callattr this, ref <imm8>, const <imm8>, #<imm8>' - Perform a supercall on `(this as ref <imm8>).operator . (const <imm8>)', using `#<imm8>' arguments from the stack. */
Expand Down
18 changes: 9 additions & 9 deletions include/deemon/compiler/assembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -1181,15 +1181,15 @@ INTDEF WUNUSED NONNULL((1)) int DCALL asm_gadjhand(struct asm_sym *__restrict ta
* This must be called before `return'-ing from within a catch/finally handler. */
INTDEF WUNUSED int DCALL asm_gunwind(void);

#define asm_private_gfunction_ii(code_cid, n_refs) \
((code_cid) <= UINT8_MAX \
? (((n_refs) - 1) <= UINT8_MAX \
? asm_putimm8_8(ASM_FUNCTION_C, (uint8_t)(code_cid), (uint8_t)((n_refs) - 1)) \
: asm_putimm8_16(ASM_FUNCTION_C_16, (uint8_t)(code_cid), (uint16_t)((n_refs) - 1))) \
: (asm_put(ASM_EXTENDED1) || \
(((n_refs) - 1) <= UINT8_MAX \
? asm_putimm16_8(ASM_FUNCTION_C, (uint16_t)(code_cid), (uint8_t)((n_refs) - 1)) \
: asm_putimm16_16(ASM_FUNCTION_C_16, (uint16_t)(code_cid), (uint16_t)((n_refs) - 1)))))
#define asm_private_gfunction_ii(code_cid, n_refs) \
((code_cid) <= UINT8_MAX \
? ((n_refs) <= UINT8_MAX \
? asm_putimm8_8(ASM_FUNCTION_C, (uint8_t)(code_cid), (uint8_t)(n_refs)) \
: asm_putimm8_16(ASM_FUNCTION_C_16, (uint8_t)(code_cid), (uint16_t)(n_refs))) \
: (asm_put(ASM_EXTENDED1) || \
((n_refs) <= UINT8_MAX \
? asm_putimm16_8(ASM_FUNCTION_C, (uint16_t)(code_cid), (uint8_t)(n_refs)) \
: asm_putimm16_16(ASM_FUNCTION_C_16, (uint16_t)(code_cid), (uint16_t)(n_refs)))))


/* Prefix instructions. */
Expand Down
9 changes: 7 additions & 2 deletions src/deemon/compiler/asm/gencall.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,13 @@ asm_gcall_func(struct ast *__restrict func,
goto err;
if (asm_putddi(func))
goto err_refv;
if (!refc) {
#ifdef CONFIG_EXPERIMENTAL_STATIC_IN_FUNCTION
ASSERT(code->co_refstaticc >= code->co_refc);
if (!refc && code->co_refstaticc <= code->co_refc)
#else /* CONFIG_EXPERIMENTAL_STATIC_IN_FUNCTION */
if (!refc)
#endif /* !CONFIG_EXPERIMENTAL_STATIC_IN_FUNCTION */
{
/* Special case: The function doesn't reference any code.
* -> In this case, we can construct the function object
* itself as a constant. */
Expand Down Expand Up @@ -257,7 +263,6 @@ asm_gcall_func(struct ast *__restrict func,
}
Dee_Free(refv);
/* Generate assembly to create (and push) the new function object. */
ASSERT(refc != 0);
if (asm_gfunction_ii((uint16_t)cid, (uint16_t)refc))
goto err_refargv;
}
Expand Down
12 changes: 8 additions & 4 deletions src/deemon/compiler/asm/genfunction.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ asm_gpush_function(struct ast *__restrict function_ast) {
goto err;
if (asm_putddi(function_ast))
goto err;
if (!refc) {
#ifdef CONFIG_EXPERIMENTAL_STATIC_IN_FUNCTION
ASSERT(code->co_refstaticc >= code->co_refc);
if (!refc && code->co_refstaticc <= code->co_refc)
#else /* CONFIG_EXPERIMENTAL_STATIC_IN_FUNCTION */
if (!refc)
#endif /* !CONFIG_EXPERIMENTAL_STATIC_IN_FUNCTION */
{
/* Special case: The function doesn't reference any code.
* -> In this case, we can construct the function object
* itself as a constant. */
Expand Down Expand Up @@ -112,9 +118,7 @@ asm_gpush_function(struct ast *__restrict function_ast) {
}
Dee_Free(refv);
/* Generate assembly to create (and push) the new function object. */
ASSERT(refc != 0);
return asm_gfunction_ii((uint16_t)cid,
(uint16_t)refc);
return asm_gfunction_ii((uint16_t)cid, (uint16_t)refc);
err_refv:
Dee_Free(refv);
err:
Expand Down
Loading

0 comments on commit 35e0287

Please sign in to comment.