Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow initialization of resources in init. #43

Merged
merged 13 commits into from
Sep 22, 2017
Merged

Allow initialization of resources in init. #43

merged 13 commits into from
Sep 22, 2017

Conversation

jonas-schievink
Copy link
Contributor

@jonas-schievink jonas-schievink commented Sep 3, 2017

Closes #37

TODO:

  • Open dependent PRs
    • Get them merged
  • Add an example (I cannot currently run any of the examples since I "only" have an stm32f401)
  • Add a cfail test ensuring that init cannot access uninitialized resources (and possibly other tests)
    • Figure out why I cannot run these tests
  • Simplify code, it's quite ugly

@perlindgren
Copy link
Collaborator

perlindgren commented Sep 3, 2017 via email

@jonas-schievink
Copy link
Contributor Author

jonas-schievink commented Sep 3, 2017

I'm hitting #42, but managed to work around that by compiling in release mode (all examples compile, haven't run any yet).

However, I still can't run the cfail tests on the host (x86-64 Linux) because there is still ARM inline assembly being used by stm32f103xx:

error: <inline asm>:2:17: error: unknown directive
                .thumb_func
                ^


error: <inline asm>:4:21: error: invalid instruction mnemonic 'b'
                    b DEFAULT_HANDLER
                    ^

Travis seems to run these tests, too, so I don't understand why that works.

Copy link
Collaborator

@japaric japaric left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this, @jonas-schievink.

The implementation looks good to me but I left some nit comments. I agree that having a cfail test for this is a good idea.

let mut mod_items = vec![];

if !app.resources.is_empty() {
// Write resources usable by `init`, if any
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stray comment?



// Are there any resources that have an initializer? Those can be used by `init`.
let has_initialized_resources = app.resources.iter()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be expressed as app.resource.keys().any(|res| res.expr.is_some()).

let mut fields = vec![];
let mut lifetime = None;
let mut rexprs = vec![];

for (name, resource) in &app.resources {
for (name, resource) in app.resources.iter()
.filter(|&(_, res)| res.expr.is_some()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I wonder if this would be more "correct" as app.resources.iter().filter_map(|(name, res)| res.map(|res| (name, rers))). It does look more complicated though. Basically what I'm trying to do is avoid having resource be an "Option" that it's always Some. In any case, we won't unwrap the Option in this branch so maybe I'm just being pendantic ...

@@ -204,6 +213,46 @@ fn init(app: &App, main: &mut Vec<Tokens>, root: &mut Vec<Tokens>) {
exprs.push(quote!(init::Resources::new()));
}

let mut late_resources = vec![];
let has_late_resources = app.resources.iter()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above: this could use any

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it just occurred to me that this could be done in a single pass over the resources by collecting the "early" resources and the "late" resources in vectors and then doing the appropriate codegen depending on whether the vector is empty or not.

Not sure which one would be easier to read.

});

late_resources.push(quote! {
#_name = #krate::UntaggedOption { some: _late_resources.#name };
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think UntaggedOption::some(..) would be shorter. Would that require the const_fn feature in user code though? Maybe it's already required by other generated code ...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, #_name.some = _late_resources.#name is even shorter. I'm not 100% if that has the exact semantics wrt to destructors. I think they are never run for any assignment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would that require the const_fn feature in user code though?

Yes. That's the reason why I made those fields public in the first place.

Actually, #_name.some = _late_resources.#name is even shorter.

Yeah, I'll do this

I'm not 100% if that has the exact semantics wrt to destructors. I think they are never run for any assignment.

Running them would be unsafe (how does the drop glue know which variant was active before the assignment?), so it can't really happen automatically (dropping or assignment is always safe). At least that's how I understand it.

},
None => quote! {
// Resource initialized in `init`
static mut #_name: #krate::UntaggedOption<#ty> = #krate::UntaggedOption { none: () };
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: as above this would be shorter as UntaggedOption::none()

src/lib.rs Outdated

use core::u8;

pub use rtfm_core::{Resource, Static, Threshold};
pub use cortex_m::asm::{bkpt, wfi};
pub use cortex_m_rtfm_macros::app;
pub use untagged_option::UntaggedOption;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This re-export should be #[doc(hidden)] as it is a not user facing implementation detail.

@jonas-schievink jonas-schievink changed the title [WIP] Allow initialization of resources in init. Allow initialization of resources in init. Sep 22, 2017
@jonas-schievink
Copy link
Contributor Author

Got rid of all iterators except a single call to partition.

@japaric
Copy link
Collaborator

japaric commented Sep 22, 2017

I had forgotten about the partition method. Nice.

@homunkulus r+

@homunkulus
Copy link
Contributor

📌 Commit be1a27c has been approved by japaric

@homunkulus
Copy link
Contributor

⌛ Testing commit be1a27c with merge be1a27c...

@jonas-schievink
Copy link
Contributor Author

This needs a new release of rtfm-syntax before landing. I've redirected the dependency to my fork to work on this, the branch is now deleted. The other failures look like something else caught fire...

@homunkulus
Copy link
Contributor

💔 Test failed - status-travis

@japaric
Copy link
Collaborator

japaric commented Sep 22, 2017

The other failures look like something else caught fire...

Xargo v0.3.9 can't compile compiler-builtins. Could you pin the version to v0.3.8? That version works fine.

@japaric
Copy link
Collaborator

japaric commented Sep 22, 2017

rtfm-syntax v0.2.0 has been released

@japaric
Copy link
Collaborator

japaric commented Sep 22, 2017

@homunkulus r+

@homunkulus
Copy link
Contributor

📌 Commit df85298 has been approved by japaric

@homunkulus
Copy link
Contributor

⌛ Testing commit df85298 with merge df85298...

@homunkulus
Copy link
Contributor

💔 Test failed - status-travis

@japaric
Copy link
Collaborator

japaric commented Sep 22, 2017

The thumbv6m build failure is due to issue #42, which has already been fixed. We are seeing the issue here because we are cross compiling the stm32f103xx crate, which is a Cortex-M3 device crate, for thumbv6m. I guess we should use a Cortex-M0 device crate for the example when cross compiling to thumbv6m.

I thought that xargo check --examples may work around the issue but it still does the linking step and errors.

As a horrible hack we could set the thumbv6m linker to the true command then linking will always work ...

Horrible hack until we switch to a Cortex-M0 device crate that works with armv6.
@jonas-schievink
Copy link
Contributor Author

I changed the linker to true. I'll open an issue to switch to some Cortex-M0 device crate when this lands.

@japaric
Copy link
Collaborator

japaric commented Sep 22, 2017

@homunkulus r+

@homunkulus
Copy link
Contributor

📌 Commit a190da3 has been approved by japaric

@homunkulus
Copy link
Contributor

⌛ Testing commit a190da3 with merge a190da3...

@homunkulus
Copy link
Contributor

☀️ Test successful - status-travis
Approved by: japaric
Pushing a190da3 to master...

@homunkulus homunkulus merged commit a190da3 into rtic-rs:master Sep 22, 2017
@jonas-schievink jonas-schievink deleted the init-resources branch September 23, 2017 14:49
japaric pushed a commit that referenced this pull request Sep 10, 2021
43: Add AfoHT to devs in README r=korken89 a=AfoHT



Co-authored-by: Henrik Tjäder <henrik@tjaders.com>
andrewgazelka pushed a commit to andrewgazelka/cortex-m-rtic that referenced this pull request Nov 3, 2021
Zero cost stack overflow protection, take 2
andrewgazelka pushed a commit to andrewgazelka/cortex-m-rtic that referenced this pull request Nov 3, 2021
this commit adds LLD support by removing all INFO sections. LLD kind of supports INFO in the form of
NOLOAD but when the linker script contains NOLOAD sections LLD emits a binary with false `size`
information: for example, it reported a fake increase of 20KB in .text and a fake increase of 1KB in
.bss when compiling a program that only allocates a single Box.

As the INFO sections are gone we can no longer support the stack overflow protection added in rtic-rs#43 so
all the other related changes, like making _stack_start overridable, have been removed as well.
If you want to continue using stack overflow protection you can stick to v0.3.x.

As the .debug_gdb_scripts output section has been removed from the linker script these changes will
only reliably support both LD and LLD if/when rust-lang/rust#49728 lands.

closes rtic-rs#53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants