Hacking C compiler

Поділитися
Вставка
  • Опубліковано 8 лис 2024

КОМЕНТАРІ • 99

  • @diegorocha2186
    @diegorocha2186 2 роки тому +10

    Imagining add 2 lines of code in a language that is 50 years old and having such a nice feature!!! kudos!

  • @batlin
    @batlin 2 роки тому +20

    On the topic of wishy washy subjective terms like "readability" and "maintainability" being used to justify painful practices with few actual benefits, I agree. I've seen "SOLID principles" spoken of like an undeniable natural law. Then you work on codebases where someone decided nearly every class needs a separate interface and implementation, and that you should have dependency injection everywhere, causing mysterious problems and extremely low developer productivity.

    • @engelsteinberg593
      @engelsteinberg593 2 роки тому +2

      It is OOP, everything that you will made will be bad, because the underlining language is fundamentally flawed.

  • @nikolaradojevic7173
    @nikolaradojevic7173 2 роки тому +43

    tcc is being developed. Recently they started working on riscv port, and it's pretty much fully working, other than assembler that is. That version just hasn't been release yet

  • @musdevfrog
    @musdevfrog 2 роки тому +13

    I'm loving the consistent uploads!

  • @MisterDan
    @MisterDan 2 роки тому +8

    Very fun to watch... we have been working on a compiler for clients so your adventures are appreciated! We decided to make everything -> in the end BTW.

  • @oj0024
    @oj0024 2 роки тому +17

    Cool stuff. I recently did something similar by adding a `__TIMER__` macro to tcc with expands to a high resolution time. I used that to benchmark preprocessor expansion.

  • @kibblepickle
    @kibblepickle 2 роки тому +7

    Thanks for making such awesome videos. Always get to learn something new or gain new perspective on things from them.

  • @krumpy8259
    @krumpy8259 2 роки тому +5

    I just love that C compiler content :D
    Even though I no way near that level of expertise, thank you tsoding :)

  • @dwightk.schrute8696
    @dwightk.schrute8696 2 роки тому +19

    13:15 Here's a trick I like to use in cases like this - you already have TCC build with debuginfo, so you can do something like `strace -k -e write ./tcc ...` the -k switch will give you stack trace with func/file information.

  • @stephaneduhamel7706
    @stephaneduhamel7706 2 роки тому +19

    Just add a warning saying this isn't in-spec and won't work with older compliers, and then there is no downside to this feature.

  • @dnkreative
    @dnkreative Рік тому +4

    they decided to add -> operator because original operators * and . precedence is broken by design, so you have to use parentheses in order to make it work: *struct_val.filed should be (*struct_val).field and after introduction of -> it became struct_val->field.

  • @kgarrison343
    @kgarrison343 2 роки тому +19

    People are really complaining about """readability""" when ever other language uses the dot to access all of a class's elements regardless of its memory semantics.

    • @berndeckenfels
      @berndeckenfels 7 місяців тому +1

      Correct, It can’t be readability since c uses . anyway

  • @FlinCode
    @FlinCode 2 роки тому +3

    bruh this guy has a super computer working in sync with his brain how does he know so much stuff

  • @lens3973
    @lens3973 2 роки тому +4

    Because new OSes require new compilers anyway, I'm thinking about hacking the tool chain for my OS in the same way as shown here. This would allow all programs written for the OS to use this feature!

  • @superscatboy
    @superscatboy 2 роки тому +13

    C and C++ haven't been language compatible for aeons. They're still ABI compatible, and that's all anyone actually cares about. I can see no reason for this addition to C to change ABI compatibility with C++.

  • @flleaf
    @flleaf 2 роки тому +1

    I was so hoping you would choose tcc and he did. Such a relief

  • @rednibcoding3412
    @rednibcoding3412 2 роки тому +18

    A series where you write a c like programming language that transpiles to c would be pretty interesting. Keeping the language as simple as possible and close to c but adding some convinience to it like order independent declarations, a better include system without the h files hell and implicit detection of multiple includes of the same file, the porth include system was pretty good and maybe a proper string datatype.

    • @ed_iz_ed
      @ed_iz_ed 2 роки тому +3

      this has exactly been one of my ideas in the recent past, fix those little issues with c and make a better c, you can look into zig for something similar

  • @doctorwin64
    @doctorwin64 2 роки тому

    Hey you! I love the way you are doing. It's so chill just to lie and see what you are doing, because it's so clear and understandable. Have a good day!

  • @TheShader3232
    @TheShader3232 Рік тому

    An interesting quirk of clang that helps with easily fixable cases like this is the flag “-fixit”. It will apply suggested fixes directly to source code, so even though it usually behaves well with simple cases like this, proceed with care! (And note that it must be passed in after the -Xclang flag)

  • @MrKristian252
    @MrKristian252 2 роки тому +10

    Curious to know if everything would still work if you search and replace all the arrows with dots in the TCC compiler

  • @rodelias9378
    @rodelias9378 2 роки тому +3

    Really nice stream! Thanks a lot!

  • @rogo7330
    @rogo7330 2 роки тому +2

    I was about to say that you will confuse where pointers to struct are and where actuall structs are. But then I realized that C is statically typed language and compiler knows that you are trying to access field of the `struct *` and not a `struct`. So it is really possible to implement, yeah.

  • @alurma
    @alurma 2 роки тому +2

    This patch is amazing

  • @Diamonddrake
    @Diamonddrake Рік тому +1

    I could watch 50 episodes of making c better like this

  • @alh-xj6gt
    @alh-xj6gt 10 місяців тому

    It is great to see the TCC "fix" I would have never thought about trying that and it is very informative to see. It was a good "reason" yet on editors like emacs or vim it seems a bit contrived. When pulling out code from main into functions, selecting them in these editors are a few keystrokes. I don't know emacs anymore for vim having the cursor on opening or closing squirely bracket v%s/foo\./foo->/g and no matter how many times "foo." is there it will be replaced. It is a blind muscle memory operation. At that point I'm code gardening anyway and renaming stuff a bit better, so I'm already doing substitutes.
    Sure the C compiler should have changed that and it probably has a historical reason why it is as it is.
    (v for visual selection. % jumps between bracket pairs. s/from/to/ substitution.)

  • @thatonegoblin7051
    @thatonegoblin7051 Рік тому +1

    Goddamn this was so satisfying

  • @flleaf
    @flleaf 2 роки тому

    33:47 i actually noticed like ten minutes ago and it was satisfying when he got back into that

  • @beast_clips_9f
    @beast_clips_9f Рік тому

    Original and engaging content, thanks.

  • @pushqrdx
    @pushqrdx 2 роки тому +1

    Some people argue that the arrow is being explicit about the indirection, while auto deref hides that fact

    • @TsodingDaily
      @TsodingDaily  2 роки тому +14

      Yeah, I agree. Some people do like to argue about useless things.

    • @pushqrdx
      @pushqrdx 2 роки тому +3

      @@TsodingDaily totally agree, and i forgot to add that i am not, i actually really like auto deref in other languages and think it should be in C

  • @oqardZ
    @oqardZ Рік тому +1

    I checked comments and it looks like nobody noticed that at about 01:00:00 he uses union (instead of struct) and therefore the program should print 70, not 69. Maybe he should have run a.out instead of main?

  • @NeZversSounds
    @NeZversSounds 2 роки тому +3

    C23 in process.

  • @pushqrdx
    @pushqrdx 2 роки тому +5

    checkout a tool called tldr, for when you forget how a certain command is used, for instance `tldr ctags`
    (i use outfielder (zig) as client, but there's also tealdeer (rust) and tldr (python))

    • @denoww9261
      @denoww9261 2 роки тому +2

      +1 for tldr, it's great to avoid searching through man pages or --help output.

    • @Lensr
      @Lensr 2 роки тому

      Can new entries be added or are they all hard-coded?
      It would be cool to be able to give docstrings to any shell command...

  • @aayushbajaj2260
    @aayushbajaj2260 2 роки тому +5

    okay, this is really hot; the way you navigate w/ a window manager + vim.
    also appreciated the zoomed in terminal.
    one addition though: could you turn on screenkeys?

    • @aayushbajaj2260
      @aayushbajaj2260 2 роки тому +2

      oh that's Emacs. was getting confused about how those buffers were working

  • @JuanchosMRL
    @JuanchosMRL 2 роки тому

    17:00: LOL, You crazy Sosing!

  • @DFPercush
    @DFPercush 2 роки тому +8

    I need this in my life. But why would it break C++?

    • @TsodingDaily
      @TsodingDaily  2 роки тому +15

      I don't know. Somebody in the chat said that.
      Tbh, I feel like at this point any change can break C++.

    • @cooper2623
      @cooper2623 2 роки тому +6

      It would break C++ because you can override the -> operator. std Smart pointers do that, for example.

    • @mss664
      @mss664 2 роки тому +10

      @@cooper2623 C is not a subset of C++, it's a different language. There is no "breaking compatibility with C++", because it's not even a thing in the first place. There is a subset of both languages compatible with each other, but a valid C++ is (obviously) not always a valid C AND a valid C is not always a valid C++. The obvious examples of what you can do in C that you cannot do in C++ are some implicit casts (eg. void* returned from malloc to a pointer to a different type) and designated initializers.

    • @DFPercush
      @DFPercush 2 роки тому +2

      @@cooper2623 Oh yeah, good point. Iterators too. Although in order to override the unary * and ->, it requires that the left hand side be a class or struct, or a reference to such. It's not possible to override a raw pointer. If you are trying to refactor code that uses an iterator, you're going to be using -> in the first place, so moving it into an extracted function wouldn't change that. But refactoring in C++ usually means passing references, not pointers, so I guess the use case isn't really the same as it is in C. So I guess what I'm saying is, it might work, but it isn't really necessary, and you'd still have to type -> sometimes.

  • @jobhernandez3174
    @jobhernandez3174 2 роки тому +1

    dang this guy is good

  • @swagatochatterjee7104
    @swagatochatterjee7104 2 роки тому

    Next up Tsoding adds Rust like mutability in C

  • @Mozartenhimer
    @Mozartenhimer 2 роки тому

    I thought ai forgot how unions worked until you realized the command was wrong. Oh I doubt myself too much.

  • @is0ss213
    @is0ss213 2 роки тому

    58:58 small tip, you could have defined the macro SDL_audio_h_ (the one in the ifndef wrapper) to not include the audio header

  • @berndeckenfels
    @berndeckenfels 7 місяців тому

    When using C I wondered about the same thing. I think it was justified with easier parser, but does that still apply for ‚C24‘. I wonder what kind of expressions could be a pointer and struct or something but can’t come up with any case.. (ptr++).a …?

  • @liebranca
    @liebranca 2 роки тому +1

    "struct, union or ur mom" c:

  • @rafagd
    @rafagd 2 роки тому +1

    47:50 - I feel the real reason is to ensure you know at a glance if the variable is a pointer or a struct when you're reading the code, which is not possible with autodereferencing. That said, that could also be the compiler's responsability as well nowadays.

  • @K5RTO
    @K5RTO 2 роки тому

    Good work, really awesome.

  • @adammontgomery7980
    @adammontgomery7980 2 роки тому +6

    I've always wondered about compatibility with small features like this. Most people are using pre-compiled binaries anyways, so if the programmer prefers it, why not?

  • @fleaspoon
    @fleaspoon 2 роки тому

    this makes me wonder why they added the arrow...

  • @rogo7330
    @rogo7330 2 роки тому +1

    About your keyboard: did you checked dmesg for messages that your keyboard was connected/disconnected? That way you will be certain where problem is: in hdwr or sftwr

  • @InfoCousins
    @InfoCousins 2 роки тому

    Just a second before you started typing "ur mom" at 17:00 I said the exact same thing :D that was hilarious to me only unfortunately. Great job here!

  • @berndeckenfels
    @berndeckenfels 7 місяців тому

    1:10:40 screams YOUR DEFINES

  • @bobdobbs8397
    @bobdobbs8397 2 роки тому +1

    You come up with the most interesting ways to tinker when programming. Any chance you'd explore writing an implementation of Refal?

  • @lamprospitsillou6325
    @lamprospitsillou6325 2 роки тому +1

    Maybe consider installing an LSP in emacs, have no idea how, i use nvim. But you could have just called `go to definition` from the LSP on `next()`

    • @Lensr
      @Lensr 2 роки тому +1

      It seems Tsoding doesn't like LSP, as Emacs has multiple very popular packages that are easy to setup for this exact purpose. `eglot` is (much) easier but less configurable, while `lsp-mode` lets you really get into the thick of it.

  • @yogonzo9552
    @yogonzo9552 2 роки тому

    58:00 strange! I'm using TCC & SDL2 without this problem under windows.

  • @nomadshiba
    @nomadshiba 2 роки тому

    thanks to vim, this video is longer

  • @davidmalesevic3577
    @davidmalesevic3577 2 роки тому

    Wait why do people hate -> so much?

  • @rogo7330
    @rogo7330 2 роки тому +3

    Arrows don't add any readability to code. It's just bussy-looking. You know type of `foo`, so why do you need to specify that `foo` is needed to be accessed via `(*foo).` or `foo->` anyway?

  • @titaniumtomato7247
    @titaniumtomato7247 2 роки тому +1

    7:29 since c++ is a superset of c, the popularity of c doesn't actually indicate the quality of c++ very well.

  • @edmondmovsisyan2224
    @edmondmovsisyan2224 2 роки тому +2

    How old are you?

  • @СергейФёдоров-щ8ш
    @СергейФёдоров-щ8ш 2 роки тому

    Да все также будет работать - это ответ на твит. Т.к. кода, который использует этот хак, нет; И ничего не сломается по этой же причине. За исключением нового, кода который Вы напишите. Поэтому, что вы тут пытались сделать, не понятно? Разве что поковырять чутка tcc. ))

  • @PersonalXZ
    @PersonalXZ 4 місяці тому

    What is Foo and bar ?

    • @andreffrosa
      @andreffrosa 3 місяці тому +1

      Names that are used for simple examples, like n is used for integers. It's an American thing, I never understood where they came from either.

    • @jamesgray6804
      @jamesgray6804 Місяць тому

      These showed up in the original programming C book and have been used in example code ever since. FUBAR, I believe is an informal military term in the us. Not sure if it is related.

  • @awlam23
    @awlam23 2 роки тому +1

    Are you from Russia? Your accent sound as Russian accent and your account was registered in Russia

    • @TsodingDaily
      @TsodingDaily  2 роки тому +4

      Yes, I'm currently in Russia, specifically Siberia. I completely understand if this is defining factor for you to decide whether to watch my content or not, but keep in mind that I do not earn any money from it.

    • @awlam23
      @awlam23 2 роки тому +3

      @@TsodingDaily nothing's wrong man. I'm also from Russia and also from Siberia. I just wanted to make sure my thoughts are right. Also it's nice to know there're such good hackers living in Russia

  • @alexandrchioroglo5612
    @alexandrchioroglo5612 2 роки тому

    Понятно!

  • @beshralghalil
    @beshralghalil 2 роки тому +1

    dicord server link ?

    • @xenonbox9393
      @xenonbox9393 2 роки тому

      go on his twitch... or you know wait for someone to give it to you: discord.gg/KehewYS

    • @TheMelopeus
      @TheMelopeus 2 роки тому +1

      go to his twitch channel

  • @nerdzftwable
    @nerdzftwable 2 роки тому

    yoo

  • @stavros222
    @stavros222 2 роки тому

    If we use ' .' As '->' then how the pc can understand what is normal object and what is in pointer mode in order to treat it differently

    • @smx75
      @smx75 2 роки тому +13

      If the compiler says that you don't use the correct one then it already knows what is a pointer or not.

    • @creativity5155
      @creativity5155 2 роки тому +5

      The computer understands that, what tsoding did is not make them the same, he just made an auto dereference via the "." when needed on struct pointers withotu the "->". Not always. semantically it is different, however functionally it remains the same.

    • @Alguem387
      @Alguem387 2 роки тому

      Cause pointer declarations are explicit ??? You always know if somet is a value or a pointer

    • @stewartzayat7526
      @stewartzayat7526 2 роки тому +1

      The computer (compiler) knows that from the type of the value that can be deduced for example from its declaration.

    • @Unit_00
      @Unit_00 2 роки тому +1

      You declare what every symbol is (eg. int* myIntPointer)

  • @itachielarbi3304
    @itachielarbi3304 2 роки тому

    Why not create simple app for example MP3 player or painting program thinks like this

    • @lens3973
      @lens3973 2 роки тому +9

      He does what he is interested in! That's what keeps the videos interesting, IMO.

  • @adamvalt6609
    @adamvalt6609 2 роки тому +1

    hey, does the original way of dereferencing structs work? for example *foo.value

    • @TsodingDaily
      @TsodingDaily  2 роки тому +1

      Why wouldn't it work?

    • @adamvalt6609
      @adamvalt6609 2 роки тому +1

      ​@@TsodingDaily just curious, but yeah, it should work. (and too lazy to check by myself :) )