Commit Graph

192716 Commits

Author SHA1 Message Date
Arthur Cohen b6b567171c attribute expansion: Fix spurious stripping of tail expression
This commit fixes the issue reported in #391, but highlights another
one, which will be reported.
2022-03-16 09:38:09 +01:00
Arthur Cohen 313e9890d8 parser: Allow parsing stmts without closing semicolon
In certain cases such as macro matching or macro expansion, it is
important to allow the parser to return a valid statement even if no
closing semicolon is given. This commit adds an optional parameter to
the concerned functions to allow a lack of semicolon those special cases
2022-03-14 16:34:50 +01:00
bors[bot] 2dfc196477
Merge #1025
1025: Fix memory corruption in generation of builtin functions r=philberty a=philberty

This patch removes the pop_fn calls since no fncontext stack is required here for these intrinsic.
More context on the issues is in the commit message.

Fixes #1024

Co-authored-by: Philip Herron <philip.herron@embecosm.com>
2022-03-14 15:03:08 +00:00
Philip Herron 7d7bc2ce38 Fix memory corruption in generation of builtin functions
When we compile normal language functions we maintain a stack of the
current function declaration and associated return addresses. This is used
while building up the GCC tree graph. When we generate builtin intrinsic
functions such as offset or size_of were missing their associated push_fn
but still performed a pop_fn on completion this resulted in a corrupt
stack which valgrind shown as bad read/writes.

This patch removes the pop_fn calls since no fncontext stack is required here for these intrinsics.

Fixes #1024
2022-03-14 12:57:01 +00:00
Arthur Cohen 02887c88f5 macros: Do not try and re-expand if depth has exceeded recursion limit
We need to limit the amount of times that macro get expanded recursively
during macro-expansion. This limits the amount of times an ASTFragment
can be visited by simply incrementing the depth when setting a fragment,
and decreasing it when taking one. This way, recursive expansion which
happens at the expansion level (instead of the matching level) will
still get caught
2022-03-14 11:21:20 +01:00
bors[bot] 41f402f0b1
Merge #1004
1004: Added column!() macro r=CohenArthur a=mvvsmk

Fixes issue #979 
1) Added the column!() macro using the LOCATION_COLUMN() from gcc_linemap
2) To-Do: add relevant test cases.

Signed-off-by : M V V S Manoj Kumar <mvvsmanojkumar@gmail.com>

The test case I added always fails, I can't figure out whether there is a problem in my test case or there is something wrong with my implementation of the column!() macro. Do let me know where I am going wrong and also if I missed something . :)


Co-authored-by: M V V S Manoj Kumar <mvvsmanojkumar@gmail.com>
2022-03-13 11:45:43 +00:00
M V V S Manoj Kumar d9a5bddb4c Added column!() macro
Addresses issue #979
1) Added the column!() macro using the LOCATION_COLUMN() from gcc_linemap
2) Added relevent test cases

Signed-off-by : M V V S Manoj Kumar <mvvsmanojkumar@gmail.com>
2022-03-13 11:42:22 +05:30
bors[bot] 8c88e8e0c9
Merge #1015 #1018
1015: Add code generation for the slice type r=philberty a=philberty

This type must respect the layout of the FatPtr type in libcore. Rust
implements slices using Rustc types in libcore and uses a neat trick.

Addresses #849

1018: builtin-macros: Add more documentation for defining builtins r=CohenArthur a=CohenArthur

`@mvvsmk` you might find this a little more clear. Sorry about the confusion!

Co-authored-by: Philip Herron <philip.herron@embecosm.com>
Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
2022-03-11 14:40:22 +00:00
bors[bot] a50fcbc2eb
Merge #1017
1017: attr-visitor: Split in its own source and header r=CohenArthur a=CohenArthur

Split up the 4000 lines rust-macro-expand.cc file containing the
AttrVisitor class and the macro expander implementation


Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
2022-03-11 14:09:01 +00:00
Philip Herron 040b2ec9a6 Add code generation for the slice type
This type must respect the layout of the FatPtr type in libcore. Rust
implements slices using Rustc types in libcore and uses a neat trick.

The slice is generated into the FatPtr which contains the pointer and
length of the slice. This is then placed into a union called Repr which
has 3 variants a mutable and immutable pointer to the FatPtr and a final
variant which is the raw FatPtr. This means we can use unsafe access to
the union to gain a pointer to the FatPtr.

Addresses #849
2022-03-11 13:54:21 +00:00
Arthur Cohen c62e9eb5ee builtin-macros: Add more documentation for defining builtins 2022-03-11 14:19:56 +01:00
Arthur Cohen 63a214618a attr-visitor: Split in its own source and header
Split up the 4000 lines rust-macro-expand.cc file containing the
AttrVisitor class and the macro expander implementation
2022-03-11 13:33:59 +01:00
bors[bot] e076823eda
Merge #1016
1016: Add missing HIR lowering for SliceTypes r=philberty a=philberty

Addresses #849


Co-authored-by: Philip Herron <philip.herron@embecosm.com>
2022-03-11 12:08:30 +00:00
Philip Herron 47ae663a3e Add missing HIR lowering for SliceTypes
Addresses #849
2022-03-11 11:44:00 +00:00
bors[bot] 6e64e6636e
Merge #1008 #1009
1008: Add const_ptr lang item mappings r=philberty a=philberty

In order to support slices, we need to be able to parse and contain
mappings for the const_ptr lang item. We do not need to do any
special handling of this lang item yet but this adds the mappings
so when we hit it we do not output an unknown lang item error.

Addresses #849 

1009: Add missing type resolution to slices and arrays r=philberty a=philberty

This adds in the missing type resolution for slices and generic slices
and arrays. Since Arrays and Slices are both covariant types just like
references and pointers for example they need to handle recursive
substitutions where their element type might be a generic type
that can bind substitution parameters such as functions and ADT's.

Addresses #849 

Co-authored-by: Philip Herron <philip.herron@embecosm.com>
2022-03-11 10:39:24 +00:00
bors[bot] dbe59a3198
Merge #1007
1007: Add missing canonicalization of slices and raw pointer types r=philberty a=philberty

This is part of my patch series for slices. This adds the missing visitors
for name canonicalization. More information in the patch, once we get
slice support in we need to start taking advantage of `@dkm's` HIR
visitor refactoring to avoid these issues with missing visitors making
simple bugs hard to track down.

Fixes #1005


Co-authored-by: Philip Herron <philip.herron@embecosm.com>
2022-03-11 10:10:58 +00:00
bors[bot] ddd087b0ef
Merge #1003
1003: Add more intrinsics and refactor how we implement them r=philberty a=philberty

This patch series implements:

1. offset
2. size_of
3. unreachable
4. abort

It removes the GCC wrapper mappings to make them much easier to implement. It also demonstrates in single commits
the implementation of each of these intrinsic to make it easy to follow in how we implement them.

Addresses #658 #849 

Co-authored-by: Philip Herron <philip.herron@embecosm.com>
2022-03-11 09:41:37 +00:00
bors[bot] 1f4e2deb31
Merge #1002
1002: macros: Add abstraction around multiple matches r=CohenArthur a=CohenArthur

Adds an extra layer of abstraction around keeping multiple matches for
the same fragment. This avoids ugly code fetching the first match in
order to get the amounf of matches given by the user, while still
allowing zero-matches to exist.

Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
2022-03-11 08:51:43 +00:00
bors[bot] 51c7cf4b97
Merge #998
998: Parse macro expansion properly r=CohenArthur a=CohenArthur

This PR adds a base for trying to parse statements or items in macro invocations. We are now able to parse multiple items / expressions / statements properly, but do not lower them properly, which is the last remaining task in #943 

New macro parsing logic:
```mermaid
flowchart TD;
    has_semi -- Yes --> stmt;
    has_semi -- No --> invocation;
    invocation -- Is Parens --> expr;
    invocation -- Is Square --> expr;
    invocation -- Is Curly --> stmt;
```

Closes #943 
Closes #959 
Closes #952 

Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
2022-03-11 08:24:43 +00:00
Arthur Cohen f02392c8b3 macros: Replace macro invocations with expanded nodes
Different parsing functions need to be called based on the context
surrounding the macro invocation. This commit adds a flowchart trying to
explain the base resolving rules

Macro expansion happens at the same level as stripping, where nodes
might get removed if they are gated behind an unmet predicate. We also
perform macro expansion during this visitor's pass.

What we can do is thus to replace macro invocations with new items that
might have resulted from macro expansion: Since we're already mutating
numerous elements by removing them if they should be stripped, we can
also add elements if they should be expanded.

This commit also "fixes" macro test cases so that they are now accepted
by the new parser, which is more strict than it should for now.

Co-authored-by: SimplyTheOther <simplytheother@gmail.com>
Co-authored-by: philberty <philip.herron@embecosm.com>
2022-03-11 09:21:29 +01:00
Arthur Cohen 9f73e827ab macros: Add abstraction around multiple matches
Adds an extra layer of abstraction around keeping multiple matches for
the same fragment. This avoids ugly code fetching the first match in
order to get the amounf of matches given by the user, while still
allowing zero-matches to exist.

Co-authored-by: philberty <philip.herron@embecosm.com>
2022-03-10 21:45:34 +01:00
Philip Herron 894e9d29ad Handle generic Slices and Arrays
Slices and Arrays are covariant types which means they can contain elements
which bind generics such as ADT or FnTypes. This means substitutions can be
recursive and this gives the typechecker a chance to handle this recursion
on these types.
2022-03-10 16:51:03 +00:00
Philip Herron a620a228c1 Add missing type-checking for slice types 2022-03-10 16:50:56 +00:00
Philip Herron a1b065050b Add const_ptr lang item mappings
const_ptr is a lang item used as part of the slice implemenation this
adds it to our mappings so we do not error with an unknown lang item.
2022-03-10 16:45:49 +00:00
Philip Herron 31413ebacf Add missing canonicalization of slices and raw pointer types
When we intercept impl blocks for slices or raw pointers we must generate
the canonical path for this for name resolution this adds in the missing
visitors which will generate the path. Previously this was defaulting to
empty path segments and then hitting an assertion when we append the
empty segment.

Fixes #1005
2022-03-10 16:42:36 +00:00
Philip Herron f057445119 Add size_of intrinsic
This is another type of intrisic since the function contains no parameters
but the argument for the size_of is the generic parameter T. Which uses
TYPE_SIZE_UNIT to get the type size in bytes. GCC will optimize the
function call away when you turn optimizations on.

Addresses #658
2022-03-10 11:47:28 +00:00
Philip Herron 796c978c48 Add builtin abort intrinsic
Addresses #658
2022-03-10 11:46:24 +00:00
Philip Herron 9e23c29cd0 Add builtin unreachable intrinsic mapping
This demonstrates how we can add in the simple intrinsics in a single
patch.

Addresses #658
2022-03-10 11:40:11 +00:00
Philip Herron 178cabde9f Add missing builtin mappings for never type 2022-03-10 11:40:11 +00:00
Philip Herron 94990a843b Refactor how we define simple intrinsics
Intrinsics were hidden behind the GCC abstract. This removes it by keeping
all of this logic within rust-intrinsic.cc so that we can make mappings of
the rustc name to GCC ones. We have a big comment from the mappings used
over to LLVM builtins which we can use to help guide how we do this for
GCC.
2022-03-10 11:40:11 +00:00
Philip Herron a08ac0c27a Add support for the rust offset intrinsic
This patch adds the initial support for generic intrinsics these are do not
map directly to GCC builtins and need to be substited with their specificed
types. This patch allows for custom implementation body for these functions
by specifying handler functions which will generate the applicable
intrinsic when asked for.

Addresses #658
2022-03-10 11:40:02 +00:00
bors[bot] 77a4950744
Merge #999
999: Refactor ABI options as part of HIR function qualifiers r=philberty a=philberty

This is a refactor to cleanup HIR::ExternBlock and HIR::FunctionQualifiers
to have an enum of ABI options to improve the error handling.

Co-authored-by: Philip Herron <philip.herron@embecosm.com>
2022-03-09 17:17:25 +00:00
bors[bot] 39c0425830
Merge #994 #997
994: Parse macro patterns properly in repetitions r=CohenArthur a=CohenArthur

Closes #966 

We actually cannot reuse functions from the parser since we're expanding a macro transcriber. This is fine as the "algorithm" is extremely simple

997: macros: Allow any delimiters for invocation r=CohenArthur a=CohenArthur

Closes #946 

Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
2022-03-08 16:31:51 +00:00
Philip Herron 749a419a2e Refactor ABI options as part of HIR function qualifiers
The AST has an ABI string as part of the function qualifiers, this was the
same in the HIR as it was initially a copy-paste. This patch changes the
HIR function qualifiers to have an enum of ABI options, during HIR lowering
the enum is setup and if an unknown ABI option is specified an error is
emitted.
2022-03-08 16:09:05 +00:00
Arthur Cohen d2a6a5eef4 macros: Allow any delimiters for invocation
It is not necessary for macro invocations to match the delimiters used
in the matcher. A matcher using parentheses can be invoked with curlies
or brackets, as well as any other combination(curlies matcher can be
invoked with parentheses or brackets)
2022-03-08 13:33:19 +01:00
Arthur Cohen dc2eab3952 macros: Add parentheses in repetition test case 2022-03-08 11:08:26 +01:00
Arthur Cohen 08b7516191 macros: Parse macro patterns properly in repetition
Co-authored-by: philberty <philip.herron@embecosm.com>
2022-03-08 11:08:26 +01:00
bors[bot] 865b6090a8
Merge #992
992: Cleanup bad unused code warnings r=philberty a=philberty

This patchset contains 4 distinct fixes:

When a constant is declared after where it is used the code-generation pass falls
back to a query compilation of the HIR::Item this did not contain a check to verify
if it was already compiled and results in duplicate CONST_DECLS being generated
if query compilation was used.

We were using a zero precision integer to contain unit-type expressions this results
in VAR_DECLS being lost in the GENERIC graph which does not allow us to perform
any static analysis upon the DECL. This changes the unit type to use an empty struct
and for initialization of a VAR_DECL we can simply pass an empty constructor and let 
GCC optimize this code for us.

Update our DEAD_CODE scan to take into account modules of items and also respect
if structures are prefixed with an underscore we can ignore generating an unused warning.

Remove our AST scan for unused code and reuse GCC TREE_USED to track wether
VAR_DECL, PARM_DECL, CONST_DECL are actually used or not. We reuse the GCC
walk_tree functions to have this as nice separate lint.

Fixes #676 

Co-authored-by: Philip Herron <philip.herron@embecosm.com>
2022-03-07 12:17:40 +00:00
Philip Herron 7820ff8b8b Remove old unused code pass this was too generic
This now uses the TREE_USED fields on GCC tree's to track the usage of
VAR_DECLS, PARM_DECLS and CONST_DECLS. The code does a pass over the body
and parameters of functions as a lint pass.

Fixes #676
2022-03-07 12:16:18 +00:00
Philip Herron e00311aa9a Update the deadcode pass to scan into modules and respect underscores on type names 2022-03-07 12:06:20 +00:00
Philip Herron be94ef6e2d Change unit-type to be an empty struct so that we do not disregard the
initilizer of variables
2022-03-07 12:06:20 +00:00
Philip Herron 7a3c935c0f Check if this constant item might already be compiled 2022-03-07 12:06:20 +00:00
bors[bot] 366c53371a
Merge #991
991: Match and expand macro separators properly r=CohenArthur a=CohenArthur

More nice recursive macros:
```rust
macro_rules! add {
    ($e:expr | $($es:expr) | *) => {
        $e + add!($($es) | *)
    };
    ($e:expr) => {
        $e
    };
}

add!(1 | 2 | 3 | 4 | 5 | 6);
```
Closes #968

This PR needs #986 to be merged first, as it depends on it for the test cases. You can skip reviewing the first two commits which are just from #986 

Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
2022-03-07 08:47:19 +00:00
bors[bot] b82408fd6a
Merge #986
986: Fix ICE on recursive macro invocation r=CohenArthur a=CohenArthur

Closes #982 

We can now do fancy lispy things!
```rust
macro_rules! add {
    ($e:literal) => {
        0 + $e
    };
    ($e:literal $($es:literal)*) => {
        $e + add!($($es)*)
    };
}
```

I've switched the order of the commits around so that the buildbot is happy

Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
2022-03-06 21:24:24 +00:00
bors[bot] e2bccf43ed
Merge #985
985: Parse macro!(); as MacroInvocation with semicolon r=CohenArthur a=CohenArthur

When parsing a macro invocation as a statement, the parser would parse
an expression and then try parsing a semicolon. Since no actual
lookahead was done (which is a good thing), we couldn't convert a
`MacroInvocation` to a `MacroInvocationSemi` after the fact.

Since, unlike function calls, macro invocations can act differently
based on whether or not they are followed by a semicolon, we actually
need to differentiate between the two up until expansion.

This commits adds a new virtual method for ExprWithoutBlock when
converting to ExprStmtWithoutBlock so that classes inheriting
ExprWithoutBlock can specify a new behavior. In the case of our
MacroInvocation class, it simply means toggling a boolean: If we're
converting a macro from an expression to a statement, it must mean that
it should contain a semicolon.

Closes #941 

Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
2022-03-06 20:50:55 +00:00
bors[bot] d89c8ccf32
Merge #990
990: Add must use attribute support r=philberty a=philberty

This is a port of the CPP front-end nodiscard attribute to be used for
must_use. It contains a patch to clean up how we handle expressions vs
statements and removes more of the GCC abstraction. Its my hope that we
can leverage more and more existing code to get the static analysis where
we want it.

Fixes #856 

Co-authored-by: Philip Herron <philip.herron@embecosm.com>
2022-03-04 11:58:45 +00:00
bors[bot] b4bd389c66
Merge #984
984: Implimented Soluion 1 and solution 2 for issue_734 r=philberty a=mvvsmk

Fixes #734 
Done :
- [x]  Remove iterate_params function
- [x] Create new get_params function

Solution 1
1) Created a new get_params function which returns the parameters.
2) Changed the references of the iterate_params to use get_params.

Solution 2
1) Added get_params2 which returns `std::vector<TyTy::BaseType*>`
2) Changed the references of the iterate_params to use get_params.

Status :  
Currently I have implemented the first solution. 

Signed-off-by : M V V S Manoj Kumar <mvvsmanojkumar@gmail.com>


Co-authored-by: M V V S Manoj Kumar <mvvsmanojkumar@gmail.com>
2022-03-04 11:30:52 +00:00
M V V S Manoj Kumar 3f2d5a720b Got rid of lambda in TyTy::FnPtr iterate_params
Fixes issue #734
1)Removed iterate_params function
2)Created a get_params function which returns std::vector& params

Signed-off-by : M V V S Manoj Kumar <mvvsmanojkumar@gmail.com>
2022-03-04 08:27:22 +05:30
Arthur Cohen 0c7e16e125 macros: Add test cases for macro repetition separators 2022-03-03 15:21:09 +01:00
Arthur Cohen 4fde21b37a macros: Substitute separator if necessary when expanding repetitions 2022-03-03 15:21:03 +01:00