adam-mcdaniel / sage

A programming language that's wise beyond its bytes!🌱🌿🪴
https://adam-mcdaniel.net/sage-website
MIT License
414 stars 15 forks source link

Optimized generated assembly for index operations #37

Closed adam-mcdaniel closed 1 year ago

adam-mcdaniel commented 1 year ago

Indexing operations are very fast for pointers, but some kinds of data that can be indexed aren't pointer types. For example ['a', 'b', 'c', 'd', 'e'][0] does not use pointers to do the address calculation, instead its pushed onto the stack and then the address calculation can be performed on the data on the stack. This was also performed for arrays that were indexed after a member access (like a.b[0]) or dereferenced arrays (like (*a)[0]) or a nested index a[0][0].

This PR tries to reference the inner expression and index the pointer by default for any expression, and then will fallback on the stack operation method.

This PR also fixes a bug where Expr::As(expr, ty) was not typechecking the inner expression, only confirming that it could be cast to the target type. This prevented the existing optimization from passing the tests before, but it has now been discovered and fixed.